次のコードはを返しますtrue
。
console.log(document.createElement('script') instanceof Element);
<iframe>
コンテキストで同じことを行うと、次のようになりますfalse
。
let iframe = document.querySelector('iframe');
iframe = iframe.contentDocument || iframe.contentWindow.document;
console.log(iframe.createElement('script') instanceof Element);
何故ですか?
それの訳は:
1)Element
実際にはwindow.Element
2)JSには、「クラス」のようなものはありません。すべて(ほぼ)がオブジェクトです。したがって、instanceofはプロトタイプの祖先をチェックします。あなたが尋ねるとき、is some DOM node instanceof Element
あなたはこれをある種に翻訳することができますsomeDOMNode.prototype === Element
。
3)window.Element !== document.querySelector('iframe').contentWindow.Element
!!!
これはtrue
期待どおりにログに記録されます。
console.log(iframe.createElement('script') instanceof document.querySelector('iframe').contentWindow.Element);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加