如何从div获取值并将其放在php中的img标签src中

时间:2022-05-16 08:45:12

value is coming from this table on clicking a row :

值来自于单击一行时该表:

<script>
    var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#imgdiv').text();
    $('#imgid').attr('src', link_base + img_name);
</script>
<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>

Selected filename is showing in a div :

选择的文件名显示在div中:

<div id="txtdiv"></div>

And within html, there is an image tag, in which I want to pass value of div (ie the name of image file) into src="" within img tag.

在html中,有一个图像标记,在这个标签中,我想要将div的值(图像文件的名称)传递给src=" img标签"。

The code which I am trying to achieve this is as shown below :

我正在努力实现这一点的代码如下所示:

<script>
  var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#imgdiv').text();
    $('#imgid').attr('src', link_base + img_name);
</script>
<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>

3 个解决方案

#1


6  

wrap your script in $(document).ready(function(){...your code ...});, this will ensure that your DOM structure is ready to process like image in your case is ready to change its src

用$(文档)包装你的脚本。ready(函数){…您的代码…});,这将确保您的DOM结构已经准备好处理,就像您的例子中的image已经准备好修改它的src一样

<script>
  $(document).ready(function(){
  var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#imgdiv').text();
    $('#imgid').attr('src', link_base + img_name);
 });
</script>

#2


1  

Move script under img - when you try to set src attribute, no image exists.

在img下移动脚本—当您尝试设置src属性时,不存在图像。

<div id="txtdiv"></div>

<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>
<!-- here or in header link jQuery -->
<script>
    var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#txtdiv').text();
    $('#imgid').attr('src', link_base + img_name);
</script>

#3


0  

Take a look at this fiddle. The code runs at onload https://jsfiddle.net/2z7ux3et/2/

看看这把小提琴。代码在onload运行,https://jsfiddle.net/2z7ux3et/2/

It's just an example, you can use any image you want:

这只是一个例子,你可以使用任何你想要的图像:

<div id="imgdiv">Information_icon.svg</div>
<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>
$(function() {
    var link_base = 'http://upload.wikimedia.org/wikipedia/commons/3/35/' ;
    var img_name = $('#imgdiv').html();
    $('#imgid').attr('src', link_base + img_name);
});

#1


6  

wrap your script in $(document).ready(function(){...your code ...});, this will ensure that your DOM structure is ready to process like image in your case is ready to change its src

用$(文档)包装你的脚本。ready(函数){…您的代码…});,这将确保您的DOM结构已经准备好处理,就像您的例子中的image已经准备好修改它的src一样

<script>
  $(document).ready(function(){
  var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#imgdiv').text();
    $('#imgid').attr('src', link_base + img_name);
 });
</script>

#2


1  

Move script under img - when you try to set src attribute, no image exists.

在img下移动脚本—当您尝试设置src属性时,不存在图像。

<div id="txtdiv"></div>

<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>
<!-- here or in header link jQuery -->
<script>
    var link_base = "<?php echo bloginfo('template_url').'/savelabel/'; ?>" ;
    var img_name = $('#txtdiv').text();
    $('#imgid').attr('src', link_base + img_name);
</script>

#3


0  

Take a look at this fiddle. The code runs at onload https://jsfiddle.net/2z7ux3et/2/

看看这把小提琴。代码在onload运行,https://jsfiddle.net/2z7ux3et/2/

It's just an example, you can use any image you want:

这只是一个例子,你可以使用任何你想要的图像:

<div id="imgdiv">Information_icon.svg</div>
<div class="bx2">
    <img id="imgid" alt="" class="imgsty" />    
</div>
$(function() {
    var link_base = 'http://upload.wikimedia.org/wikipedia/commons/3/35/' ;
    var img_name = $('#imgdiv').html();
    $('#imgid').attr('src', link_base + img_name);
});