【问题标题】:Create new image element using the src of image in another div using jquery使用 jquery 在另一个 div 中使用图像的 src 创建新的图像元素
【发布时间】:2014-03-20 13:56:51
【问题描述】:
<div id='show-image'></div>
<div class='post'>
<div class='inner'>
<img class='post-img' src='http://3.bp.blogspot.com/-Sg5t3utxRzc/UwgyzbLVAAI/AAAAAAAAFBo/vYQX0Cphx8U/s1600/indian-bride.jpg'/>
</div>
</div>

好的,我想要的是我想要获取图像 .post-img 的 src 并想要在 div id='show-image' 中创建一个新的图像元素

如果有人可以帮助我使用 jQuery 实现这一目标,我会很高兴

【问题讨论】:

    标签: jquery src attr


    【解决方案1】:

    您可以使用 .attr() 获取图像的 src 然后 .append() 将新创建的图像与检索到的 src 附加到 div idshow-image:

    var src = $('.post-img').attr('src');
    $('#show-image').append('<img src="' + src + '" />');
    

    或者你可以使用.appendTo():

    var url=$('.post-img').attr("src"); 
    $('<img src="'+url+'" />').appendTo('#show-image');
    

    【讨论】:

    • 嗨! felix 我又打扰你了,但我想知道如果 img 标签被 iframe 替换,同样的条件也适用。
    【解决方案2】:

    只是另一种写法

    var newImg = $("<img>", { 
                    src: $(".post-img").attr("src") 
                });
    $("#show-image").append(newImg);
    

    【讨论】:

      【解决方案3】:

      试试这个:

      var img = $('<img>');
      img.attr('src', $("img.post-img").attr("src"));
      img.appendTo('#show-image');
      

      DEMO

      【讨论】:

      • @user3014202: 更新了我的答案兄弟:)
      猜你喜欢
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多