javascript contains方法は元素が関係を含むことを判断します.

2237 ワード

IE  contains      A      B  ,       true,    false,                。W3C    compareDocumentPosition,      :JS  contains compareDocumentPosition     DOM      ,      (  )            。

IE contains     ,  W3C DOMElement.contains(DOMNode)           。

DOMElement.contains(DOMNode)



link:https://developer.mozilla.org/En/DOM/Node.compareDocumentPosition。



DOMElement.contains(DOMNode)          ,          ,               :

Bits     Number     Meaning

000000     0         

000001     1             (         )

000010     2        B     A   

000100     4        A     B   

001000     8        B      A

010000     16        A      B

100000     32             





  ,  @          :



var contains  = function(root, el) {

     if (root.compareDocumentPosition)

         return root === el || !!(root.compareDocumentPosition(el) & 16);

     if (root.contains && el.nodeType === 1){

         return root.contains(el) && root !== el;

     }

     while ((el = el.parentNode))

         if (el === root) return true;

     return false;

 }