javascript 使用jQuery将img元素复制到另一个div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18702750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying img element to another div using jQuery
提问by sergserg
I have a have simple div with a single image. I want to copy that same img
element to another div.
我有一个带有单个图像的简单 div。我想将相同的img
元素复制到另一个 div。
$('.copy').click(function() {
imageElement = $(this).siblings('img.source')[0];
item = $('<div class="item">' + imageElement + '</div>');
});
I'm getting this:
我得到这个:
[object HTMLImageElement]
Instead of the actual image tag rendering. Any ideas?
而不是实际的图像标签渲染。有任何想法吗?
回答by maverickosama92
try this:
试试这个:
$("#btnCopy").on("click",function(){
var $img = $("#firstDiv").children("img").clone();
$("#secondDiv").append($img);
});
working fiddle here: http://jsfiddle.net/GbF7T/
在这里工作小提琴:http: //jsfiddle.net/GbF7T/
回答by Derek
回答by Sree
Try this
试试这个
$('.copy').click(function() {
var imageElement = $('#div1').html();
$('#div2').html(imageElement);
}
If you don't need to replace content in #div2 use "append" or "prepend" instead of html. Eg : $('#div2').prepend(imageElement);
如果您不需要替换#div2 中的内容,请使用“append”或“prepend”而不是 html。例如: $('#div2').prepend(imageElement);
回答by sudhAnsu63
try this.
试试这个。
$('.copy').click(function() {
imageElement = $(this).siblings().find('img').eq(0);
$("div.item").append(imageElement.clone());
});