Node.comparedocumentposition(otherNode)

Tags
webapixml
Created
Nov 23, 2018 7:34 AM

掩码(Mask)在计算机学科及数字逻辑中指的是一串二进制数字,通过与目标数字的按位操作(如 &),达到屏蔽指定位而实现需求。

https://dom.spec.whatwg.org/#dom-node-comparedocumentposition

返回掩码:

  • DOCUMENT_POSITION_DISCONNECTED (1); // 000001
  • DOCUMENT_POSITION_PRECEDING (2); // 000010
  • DOCUMENT_POSITION_FOLLOWING (4); // 000100
  • DOCUMENT_POSITION_CONTAINS (8); // 001000
  • DOCUMENT_POSITION_CONTAINED_BY (16, 10 in hexadecimal); // 010000
  • DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC (32, 20 in hexadecimal). // 100000

https://www.zhangxinxu.com/wordpress/2019/03/node-comparedocumentposition-api/

// 返回值是 10,8 + 2

document.body.compareDocumentPosition(document.documentElement);

所以比较节点位置关系的时候需要使用 & :

if (document.body.compareDocumentPosition(document.documentElement) & Node.DOCUMENT_POSITION_CONTAINS) {}
SuperMade with Super