jQuery - 是否可以复制和粘贴HTML?
从一个例子开始,如果我有这行HTML:
<div id="divToMove">
... somethings like 100 lines of code ...
</div>
Run Code Online (Sandbox Code Playgroud)
我想知道我是否可以使用这个div并复制和粘贴很多次...
我试图把一个jQuery/JS函数从javascript"添加"到HTML页面,但似乎这个过程太慢了.也许复制和粘贴(如果可能)更快......有些帮助吗?谢谢
Tow*_*own 18
像这样的东西?
HTML
<input type="button" id="copy" value=" copy "/>
<div id="content">
<span>here's a span</span>
<div>and a div</div>
</div>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$(function() {
$('#copy').click(function(){
var content = $('#content').html();
var newdiv = $("<div>");
newdiv.html(content);
$('#content').after(newdiv);
});
});
Run Code Online (Sandbox Code Playgroud)
在行动: http ://jsfiddle.net/Town/5q2CK/
查看.clone(),你可以在点击目标div之后做一些事情:
$('#targetDiv').click(function(){
var cloned = $(this).clone();
// now you could put at the end another div
// $('#myOtherDiv').append(cloned);
// or put insert after another div
// $(cloned).insertAfter('#myOtherDiv');
});
Run Code Online (Sandbox Code Playgroud)