怎样获取当前节点的顶层文档节点

Node.prototype.ownerDocument

虽然现在还没碰到, 但以后可能出现无法直接获取 document 节点的情况, 这时可以通过任意节点获取到它.

document.scripts[0].ownerDocument === document;
// true

document.ownerDocument;
// null

Node.prototype.getRootNode()

这个方法的作用和 Node.prototype.ownerDocument 是相同的, 唯一的不同点是这个方法对document使用时可以正常返回, 而非null.

document.getRootNode() // document
document.ownerDocument // null