DOMNode.appendChild 如何添加另一个DOM里的节点?

2013-11-20 20:50:27 +08:00
 fuxkcsdn
$dom = new DOMDocument();
$dom->loadHTML("<html><body><div>111</div></body></html>");

$dom2 = new DOMDocument();
$dom2->loadHTML("<html><body></body></html>");

$body = $dom2->getElementsByName("body")->item(0);

$body->appendChild($dom->getElementsByName("div")->item(0));

这样会提示
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error' in test.php:10
Stack trace:
#0 test.php(10): DOMNode->appendChild(Object(DOMElement))
#1 {main}
thrown in test.php on line 10

我是获取到一个页面后,只想保留其中一个table里的特定信息,所以想说用一个新的document里装,但是一直提示这错误
2662 次点击
所在节点    PHP
1 条回复
dorentus
2013-11-20 23:41:47 +08:00
http://stackoverflow.com/a/1759185/90172
[Google「Wrong Document Error」的第一条就是]

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/89970

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX