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

本文标签: