【发布时间】:2017-07-16 14:44:28
【问题描述】:
我正在尝试将一个节点(带有子节点)从导入的 html 的 sn-p 附加到现有的 html DOM 文档的正文中。我一直在尝试让节点附加到正确的位置。
这是我的代码:
$lastnode = $loaded_file->getElementsByTagName('body')->item(0)->lastChild;
$newnode = $editor_doc->getElementsByTagName('body')->item(0)->firstChild;
$newnode = $loaded_file->importNode($newnode, True);
$lastnode->appendChild($newnode);
$loaded_file 是一个使用 loadHTML 作为 DOMDocument 加载的 html 文件。
$editor_doc 是 html sn-p 也使用 loadHTML 加载为 DOMDocument
输出如下:
[Entire document of $loaded_file ending with </html>]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>1974
[the correct html snippet]
</p></body></html>
2 个问题: 1)sn-p 放错地方了。我以为它是作为body标签的孩子附加的,我做错了吗? 2) 我不知道额外的 !DOCTYPE、html、body p 和 1974 文本来自哪里。我的代码中没有任何内容可以添加。当我在任何东西上使用 appendChild 时似乎会出现。
我做错了什么?
提前致谢!
【问题讨论】: