2
這吹我的腦海裏,檢查出來:IFRAME +的instanceof對象今天失敗
的index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>framed</title>
</head>
<body>
<iframe src="sandbox2.html" frameborder="0" width="640" height="480" id="frame"></iframe>
</body>
</html>
sandbox2.html(iframe的內部)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>instanceof Object FAIL DEMO</h1>
<div id="foo"></div>
#foo.bar instanceof Object: <span id="result"></span>
<br/>
typeof #foo.bar: <span id="result2"></span>
<script>
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('foo').bar = { test: 'aaa' };
setInterval(function() {
document.getElementById('result').textContent = (document.getElementById('foo').bar instanceof Object).toString();
document.getElementById('result2').textContent = (typeof document.getElementById('foo').bar).toString();
}, 100);
});
</script>
</body>
</html>
現在,打開index.html,轉到開發者控制檯,然後輸入
document.getElementById("frame").contentDocument.getElementById('foo').bar = {}
typeof #foo.bar
回報object
(和Foo實際上是一個Object)
但#foo.bar instanceof Object
回報假!!!!
我試過Chrome,Firefox和MS Edge,都有相同的行爲。這是爲什麼發生?這是一種錯誤嗎?
是什麼'#foo.bar的instanceof Object'意味着真正的JavaScript –
'的document.getElementById( '富')。酒吧的instanceof Object' 這是正確的,在sandbox2.html –
,當我創建兩個文件如你做了,輸出是'#foo.bar instanceof Object:true' 'typeof#foo.bar:object' - 是你期望的嗎? –