javascript,如何将DOMparser与text / html一起使用时如何删除
元素>
代码
var txt = '<div id="hi">fe</div><div id="h2">fe</div><div id="hj">fe</div>' var parser = new DOMParser(); var temp_node = parser.parseFromString(txt, "text/html").documentElement; console.log(temp_node)
此代码生成完整的html文档,其中包括
<html><head></head><body> <div id="hi">fe</div> <div id="h2">fe</div> <div id="hj">fe</div> </body></html>
如果我只想要<div id="hi">fe</div><div id="h2">fe</div><div id="hj">fe</div>部分怎么办?我该怎么办?
而且,如果我想附加所有节点,有没有办法做到没有循环?
parentNode.appendChile(temp_node) // add the entire code parentNode.appendChile(temp_node.firstElementChild.nextElementSibling) // add the parent <body> and the other layers inside parentNode.appendChild(temp_node.firstElementChild.nextElementSibling.childNodes) // doesn't do the trick, it complains about not being a "node", I guess I'd need an "appendChilds" function that allows to add many nodes at once
**我希望的,如果parentNode是<div id="parent">
<div id="parent"> <div id="hi">fe</div> <div id="h2">fe</div> <div id="hj">fe</div> </div>
但是我知道
<div id="parent"> <body> <div id="hi">fe</div> <div id="h2">fe</div> <div id="hj">fe</div> </body> </div>
代码var txt ='
fe
fe
fe
'var parser = new DOMParser(); var temp_node = parser.parseFromString(txt,“ ...