最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript, how to remove the <html><head><body> elements when using DOMparser with te

programmeradmin8浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 9

Use 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);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论