谷歌浏览器不接受 .contentDocument 或 .contentWindow

jhf*_*ire 3 javascript iframe google-chrome dom-events

当我尝试我的代码时:

测试.html -

<script language="JavaScript" type="text/javascript">
function find() { 
var iframeEl = document.getElementById('love');
if ( iframeEl.contentDocument ) {
    var form = iframeEl.contentDocument.document.getElementById('hi').getAttribute('href');
    alert(form)
} else if ( iframeEl.contentWindow ) {
    var form = iframeEl.contentWindow.document.getElementById('hi').getAttribute('href');
    alert(form)
} 
  }
</script>
<body onload="find()">
<iframe name="lovez" src="frame.html" id="love"><a href="http://www.google.com" id="hi">Testingz</a></iframe>
</body>
Run Code Online (Sandbox Code Playgroud)

Frame.html -

<a href="http://www.google.com" id="hi">Testing</a>
Run Code Online (Sandbox Code Playgroud)

它不会返回警告框。但是在 Internet Explorer 上它会。我一直在互联网上搜索,尝试所有示例,但找不到可以在 Google Chrome 中使用的简单示例。我做错了什么还是只是谷歌浏览器?

Isr*_*Gav 6

我注意到如果代码来自 Web 服务器,则contentDocument和getSVGDocument(对于来自 svg 文件的 svg 对象)都可以正常工作,但是当我将代码复制到本地文件时,它将无法正常工作。

这是一个示例链接,其中包含我从中复制的代码,该代码可在网络上运行,但不能从我的磁盘中运行。

这是我刚刚从 Chrome 找到的解决方案(感谢Artem S),即使用--allow-file-access-from-files标志。