javascript - When do I need to call importNodeadoptNode? - Stack Overflow

On the MDN page for importNode, it says:Before they can be inserted into the current document, nodes fr

On the MDN page for importNode, it says:

Before they can be inserted into the current document, nodes from external documents should either be:

cloned using document.importNode(); or

adopted using document.adoptNode().

It's been a while since I've done anything involving multiple documents, and maybe I'm a little rusty, but I always thought the should in the snippet above was an indicator that you'd get an exception if you tried to add a foreign element without calling either function first.

I tried this in Edge's console and there didn't seem to be a problem moving nodes between documents.

let a = new Document();
let b = new Document();
b.appendChild(b.createElement("div"));
b.childNodes;
> NodeList [div]
b.firstChild.ownerDocument === b
> true
a.appendChild(b.firstChild);
a.childNodes;
> NodeList [div]
a.firstChild.ownerDocument === a
> true
b.childNodes;
> NodeList []

I seem to recall browsers being more hostile to this kind of manoeuvring in the past. Under what circumstances should I expect this to not work and give me an exception? Is it because there are no namespace differences? Are nodes auto-adopted? Have I made the Document objects the wrong way?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742296480a4417222.html

相关推荐

  • javascript - When do I need to call importNodeadoptNode? - Stack Overflow

    On the MDN page for importNode, it says:Before they can be inserted into the current document, nodes fr

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信