outerHTML attribute use cases

ask:

How to reverse this line of code const doc = parser.parseFromString(newListString, 'text/html') and turn doc into newListString

answer:

If you want to convert a DOM object back to an HTML string, you can use the `outerHTML` attribute from the DOM standard. This property returns an HTML markup string representing the current node and its child nodes.

The following is a sample code that demonstrates how to convert a DOM object into an HTML string:

```javascript
const parser = new DOMParser();
const htmlString = '<div>This is a div element</div>';
const doc = parser.parseFromString(htmlString, 'text/html');

// Get the outerHTML attribute of doc, that is, the HTML string
const newListString = doc.documentElement.outerHTML;
console.log(newListString); // Output: '<html><head></head><body><div>This Is a div element </div></body></html>

おすすめ

転載: blog.csdn.net/kuang_nu/article/details/130972897