Skip to content

Instantly share code, notes, and snippets.

@xyqfer
Created September 28, 2013 07:13
Show Gist options
  • Save xyqfer/6739429 to your computer and use it in GitHub Desktop.
Save xyqfer/6739429 to your computer and use it in GitHub Desktop.
确定HTML节点间关系
function contains(parentNode, childNode) {
if (parentNode.contains) {
return parentNode != childNode && parentNode.contains(childNode);
} else {
return !!(parentNode.compareDocumentPosition(childNode) & 16);
}
}
/*
NodeA.compareDocumentPosition(NodeB):
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 浏览器的私有使用
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment