How can I determine if a dynamically-created DOM element has been added to the DOM?

后端 未结 11 979
滥情空心
滥情空心 2020-12-02 20:07

According to spec, only the BODY and FRAMESET elements provide an \"onload\" event to attach to, but I would like to know when a dynamically-create

11条回答
  •  离开以前
    2020-12-02 20:41

    Here is another solution to the problem extracted from the jQuery code. The following function can be used to check if a node is attached to the DOM or if it is "disconnected".

    function isDisconnected( node ) {
        return !node || !node.parentNode || node.parentNode.nodeType === 11;
    }
    

    A nodeType === 11 defines a DOCUMENT_FRAGMENT_NODE, which is a node not connected to the document object.

    For further information see the following article by John Resig: http://ejohn.org/blog/dom-documentfragments/

提交回复
热议问题