How to check if an element is inside an iframe or

2019-08-08 20:58发布

问题:

Let's say you have a DOM node and you want to know whether it is located inside an iframe or not. One way would be to check it's parent chain to see if you reached an iframe before reaching the parent window. However, I'd like to know if there is a faster way to do this.

回答1:

You could probably check the ownerDocument property of the node:

if(node.ownerDocument !== document) {
    // node must be inside iframe
}