admin管理员组文章数量:1287983
The code
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)
This code results in the full html document, this is including
<html><head></head><body>
<div id="hi">fe</div>
<div id="h2">fe</div>
<div id="hj">fe</div>
</body></html>
What if I want only the <div id="hi">fe</div><div id="h2">fe</div><div id="hj">fe</div>
part? How can I do it?
And, if I want to append all nodes, is there a way to do it without a loop?
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 plains about not being a "node", I guess I'd need an "appendChilds" function that allows to add many nodes at once
*What I'd wish, if parentNode is <div id="parent">
<div id="parent">
<div id="hi">fe</div>
<div id="h2">fe</div>
<div id="hj">fe</div>
</div>
But I get
<div id="parent">
<body>
<div id="hi">fe</div>
<div id="h2">fe</div>
<div id="hj">fe</div>
</body>
</div>
The code
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)
This code results in the full html document, this is including
<html><head></head><body>
<div id="hi">fe</div>
<div id="h2">fe</div>
<div id="hj">fe</div>
</body></html>
What if I want only the <div id="hi">fe</div><div id="h2">fe</div><div id="hj">fe</div>
part? How can I do it?
And, if I want to append all nodes, is there a way to do it without a loop?
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 plains about not being a "node", I guess I'd need an "appendChilds" function that allows to add many nodes at once
*What I'd wish, if parentNode is <div id="parent">
<div id="parent">
<div id="hi">fe</div>
<div id="h2">fe</div>
<div id="hj">fe</div>
</div>
But I get
<div id="parent">
<body>
<div id="hi">fe</div>
<div id="h2">fe</div>
<div id="hj">fe</div>
</body>
</div>
Share
Improve this question
edited Aug 28, 2015 at 23:11
GWorking
asked Aug 28, 2015 at 22:45
GWorkingGWorking
4,34111 gold badges57 silver badges96 bronze badges
2 Answers
Reset to default 9Use childNodes
console.log(temp_node.childNodes[1].childNodes[0]);
or querySelector
console.log(temp_node.querySelector("#hi"));
JSFiddle demo
Update
or innerHTML
console.log(temp_node.querySelector("body").innerHTML);
JSFiddle demo
You can use .body
instead:
parser.parseFromString(txt, "text/html").body
When you use the property .documentElement
as you did in your code, it returns the following:
Returns the Element that is a direct child of the document. For HTML documents, this is normally the HTMLHtmlElement object representing the document's
<html>
element.- MDN
Other properties exist on the Document, .body
is one of which. Using .body
(instead of querySelector()
) will give you fast and direct access to the body
of your HTML content, which you can then use .innerHTML
on to get its inner contents as a string:
See working example:
const txt = '<div id="hi">fe</div><div id="h2">fe</div><div id="hj">fe</div>'
const parser = new DOMParser();
const temp_node = parser.parseFromString(txt, "text/html").body;
console.log(temp_node.innerHTML);
本文标签:
版权声明:本文标题:javascript, how to remove the <html><head><body> elements when using DOMparser with te 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741066234a2333603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论