如何将body元素添加到空的DOM文档? [英] How can I add a body element to an empty DOM document?
本文介绍了如何将body元素添加到空的DOM文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但我似乎不能够将身体添加到文档中。在chrome中,以下代码在第2行中失败,其中
NO_MODIFICATION_ALLOWED_ERR:DOM异常7
。 var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml','html',null);
dom.firstChild.innerHTML =< body>< p> Hello world< / p>< / body>;
有没有办法实现我想要的?
解决方案
不可能编辑文档的根元素的innerHTML,但是可以这样做一个子节点。所以,这样做:
var dom = document.implementation.createDocument('http://www.w3.org/1999 / xhtml','html',null);
var body = dom.createElement(body);
body.innerHTML =< p> hello world< / p>;
dom.firstChild.appendChild(body);
I have this string that represents the body of a page, which I would like to parse for some elements. I believe (feel free to contradict me) the best way to do so is to create an empty document, then add the body and use standard JS methods to get what I want.
But I don't seem to be able to add the body to the document. In chrome, the following code fails on line 2 with NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7
.
var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
dom.firstChild.innerHTML = "<body><p>Hello world</p></body>";
Is there any way to achieve what I want?
解决方案
It's not possible to edit the innerHTML of the document's root element, but doing so of a child node is possible. So, this works:
var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
var body = dom.createElement("body");
body.innerHTML = "<p>hello world</p>";
dom.firstChild.appendChild(body);
这篇关于如何将body元素添加到空的DOM文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文