V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fuxkcsdn
V2EX  ›  PHP

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

  •  
  •   fuxkcsdn · 2013-11-20 20:50:27 +08:00 · 2656 次点击
    这是一个创建于 3801 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $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里装,但是一直提示这错误
    1 条回复    1970-01-01 08:00:00 +08:00
    dorentus
        1
    dorentus  
       2013-11-20 23:41:47 +08:00   ❤️ 1
    http://stackoverflow.com/a/1759185/90172
    [Google「Wrong Document Error」的第一条就是]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4609 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:01 · PVG 18:01 · LAX 03:01 · JFK 06:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.