简体   繁体   English

用文档片段javascript替换元素内容

[英]Replace element contents with document fragment javascript

I'm trying to replace all contents of an element with a document fragment:我正在尝试用文档片段替换元素的所有内容:

var frag = document.createDocumentFragment()

The document fragment is being created just fine.正在创建文档片段就好了。 No problems there.没有问题。 I add elements to it just fine, no problems there either.我向它添加元素就好了,那里也没有问题。 I can append it using element.appendChild(frag) .我可以使用element.appendChild(frag)附加它。 That works just fine too.这也很好用。

I'm trying to create a "replace" method similar to jQuery's HTML.我正在尝试创建一个类似于 jQuery 的 HTML 的“替换”方法。 I'm not worried about old-browser compatibility.我不担心旧浏览器的兼容性。 Is there a magical function to replace all content of an element?有没有神奇的功能来替换元素的所有内容?

I have tried element.innerHTML = frag.cloneNode(true) , (as per every 'replace element content' wiki I could find), that doesn't work.我试过element.innerHTML = frag.cloneNode(true) ,(根据我能找到的每个“替换元素内容”维基),这不起作用。 It gives me <div>[object DocumentFragment]</div> .它给了我<div>[object DocumentFragment]</div>

No libraries, please, not even a jQuery solution.没有库,请,甚至没有 jQuery 解决方案。

For clarity, I'm looking for a "magic" solution, I know how to remove all the existing elements one at a time and then append my fragment.为清楚起见,我正在寻找一种“神奇”的解决方案,我知道如何一次删除所有现有元素,然后附加我的片段。

Have you tried replaceChild你有没有试过replaceChild

something like this像这样的东西

element.parentNode.replaceChild(frag, element)

source: https://developer.mozilla.org/en-US/docs/DOM/Node.replaceChild来源: https : //developer.mozilla.org/en-US/docs/DOM/Node.replaceChild

original jsFiddle: http://jsfiddle.net/tomprogramming/RxFZA/原始 jsFiddle: http : //jsfiddle.net/tomprogramming/RxFZA/

EDIT: ahh, I didn't see replace contents.编辑:啊,我没有看到替换内容。 Well, just remove them first!好吧,只需先删除它们!

element.innerHTML = "";
element.appendChild(frag);

jsfiddle: http://jsfiddle.net/tomprogramming/RxFZA/1/ jsfiddle: http : //jsfiddle.net/tomprogramming/RxFZA/1/

note that in the jsfiddle, I only use jquery to hook up the button, the entirety of the click handler is raw javascript.请注意,在 jsfiddle 中,我只使用 jquery 来连接按钮,整个点击处理程序都是原始 javascript。

Edit2: also suggested by pimvdb, but just append the new stuff to a detached element and replace. Edit2:也由 pimvdb 建议,但只需将新内容附加到分离的元素并替换。

var newElement = element.cloneNode();
newElement.innerHTML = "";
newElement.appendChild(frag);
element.parentNode.replaceChild(newElement, element);

http://jsfiddle.net/tomprogramming/RxFZA/3/ http://jsfiddle.net/tomprogramming/RxFZA/3/

2017: 2017年:
Try this Magic answer from ContentEditable field and Range从 ContentEditable 字段和Range尝试这个神奇的答案

var range = document.createRange(); // create range selection 
range.selectNodeContents($element); // select all content of the node
range.deleteContents() // maybe there is replace command but i'm not find it
range.insertNode(frag)

EDIT (cause my original answer was just plain dumb):编辑(因为我原来的答案是愚蠢的):

var rep = document.createElement("div");
rep.appendChild(frag);
element.innerHTML = rep.innerHTML;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM