fuxkcsdn
V2EX  ›  PHP

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

  •  
  •   fuxkcsdn · Nov 20, 2013 · 3258 views
    This topic created in 4623 days ago, the information mentioned may be changed or developed.
    $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 replies    1970-01-01 08:00:00 +08:00
    dorentus
        1
    dorentus  
       Nov 20, 2013   ❤️ 1
    http://stackoverflow.com/a/1759185/90172
    [Google「Wrong Document Error」的第一条就是]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2314 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:59 · PVG 08:59 · LAX 17:59 · JFK 20:59
    ♥ Do have faith in what you're doing.