jQuery - 是否可以复制和粘贴 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5670950/
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
jQuery - Is it possible copy and paste HTML?
提问by markzzz
jQuery - Is it possible copy and paste HTML?
jQuery - 是否可以复制和粘贴 HTML?
Starting with an example, if I have this lines of HTML :
从一个例子开始,如果我有这行 HTML :
<div id="divToMove">
... somethings like 100 lines of code ...
</div>
i'd like to know if I can take this div and copy and paste many times...
我想知道我是否可以将这个 div 复制并粘贴多次...
I tried to put a jQuery/JS function that "add" this code from javascript to the HTML page, but seems that is too slower as process. Maybe a copy and paste (if possible) is faster... Some helps? Thanks
我试图将一个 jQuery/JS 函数从 javascript 中“添加”这段代码到 HTML 页面,但似乎作为过程太慢了。也许复制和粘贴(如果可能)会更快......有些帮助?谢谢
回答by Town
Something like this?
像这样的东西?
HTML
HTML
<input type="button" id="copy" value=" copy "/>
<div id="content">
<span>here's a span</span>
<div>and a div</div>
</div>
jQuery
jQuery
$(function() {
$('#copy').click(function(){
var content = $('#content').html();
var newdiv = $("<div>");
newdiv.html(content);
$('#content').after(newdiv);
});
});
In action:http://jsfiddle.net/Town/5q2CK/
回答by Ady Ngom
look into .clone() and you could do things like after clicking on the target div:
查看 .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');
});
回答by jrlmx2
You should be able to do something like
你应该能够做类似的事情
var html = $( '#divToMove' ).html();
This will take the contents of the div and put it into the variable html. You can then turn around and
这将获取 div 的内容并将其放入变量 html 中。然后你可以转身
$( '#paste' ).append( html );
to add the 'copied' html to the end of that location. There is also a prepend function to add the html to the beginning of a container
将“复制”的 html 添加到该位置的末尾。还有一个 prepend 函数可以将 html 添加到容器的开头
$( '#paste' ).prepend( html );
This is the only way I know of to "copy and paste" html. One thing to note, .html() will grab the contents of the selector and not the element the selector is pointing at.
这是我所知道的“复制和粘贴”html 的唯一方法。需要注意的一件事是, .html() 将抓取选择器的内容,而不是选择器指向的元素。
回答by sym3tri
Copy and paste is intentionally not possible in the browser via JavaScript. It is a security restriction
有意无法通过 JavaScript 在浏览器中进行复制和粘贴。这是一个安全限制
回答by Scherbius.com
yes it is possible! Following code copies HTML from div "x" to div "y":
对的,这是可能的!以下代码将 HTML 从 div "x" 复制到 div "y":
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<div id="x">
This is a test
<span>this is another text</span>
</div>
<div id="y">
</div>
<script type="text/javascript">
var x = document.getElementById("x"),
y = document.getElementById("y");
y.innerHTML = x.innerHTML;
</script>
</body>
</html>
回答by Vishnu Bhadoriya
copy and paste on-click button jquery code
复制并粘贴点击按钮 jquery 代码
<button id="copybtn">Copy</button>
<div id="toCopy">Content To Be Copied</div>
<div id="toPaste"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$("#copybtn").click(function(){
$('#toPaste').append($("#toCopy").html());
});
</script>