谁能告诉我不用jQuery怎么使用这两个函数?
我使用的是一个预编码的应用程序,我不能在其中使用jQuery,我需要从一个div中取出HTML,然后使用JS将其移动到另一个div中。
发布于 2011-05-26 17:02:19
要将HTML从一个div复制到另一个div,只需使用DOM即可。
function copyHtml(source, destination) {
var clone = source.ownerDocument === destination.ownerDocument
? source.cloneNode(true)
: destination.ownerDocument.importNode(source, true);
while (clone.firstChild) {
destination.appendChild(clone.firstChild);
}
}
对于大多数应用程序,inSameDocument
总是会是真的,所以当它是假的时候,你可能可以省略掉所有起作用的部分。如果您的应用程序在同一个域中有多个框架通过JavaScript进行交互,您可能希望将其保留在其中。
如果您想替换HTML,您可以通过清空目标然后复制到其中来实现:
function replaceHtml(source, destination) {
while (destination.firstChild) {
destination.removeChild(destination.firstChild);
}
copyHtml(source, destination);
}
发布于 2011-05-26 17:04:45
你可以替换
var content = $("#id").html();
使用
var content = document.getElementById("id").innerHTML;
和
$("#id").append(element);
使用
document.getElementById("id").appendChild(element);
发布于 2016-11-25 21:22:30
晚了几年才参加派对,但不管怎样,这里有一个解决方案:
document.getElementById('your-element').innerHTML += "your appended text";
这对于将html附加到dom元素非常有效。
https://stackoverflow.com/questions/6136012
复制相似问题