javascript Google Chrome 中的 getElementById 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3803719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:17:59  来源:igfitidea点击:

getElementById problem in Google Chrome

javascriptgoogle-chrome

提问by tinti

I have this simple line

我有这条简单的线

alert(window.parent.frames[0].document.getElementById('textToSearch').value);

I have 2 frames, first with a text field with id 'textToSearch' I want to get the value of the text field in the second frame The line above is on the html file from second frame I get this error only in Google Chrome, in IE or FF works fine.

我有 2 个框架,第一个文本字段的 id 为 'textToSearch' 我想在第二个框架中获取文本字段的值上面的行是第二个框架的 html 文件我只在谷歌浏览器中得到这个错误,在IE 或 FF 工作正常。

Uncaught TypeError: Cannot call method 'getElementById' of undefined

未捕获的类型错误:无法调用未定义的方法“getElementById”

Any ideas?

有任何想法吗?

Thanks in advance

提前致谢

采纳答案by tinti

Finally i figure what was the problem. I try the code from above on Google Chrome on a system local file. Due the security settings of Google Chrome this usecase is impossible. If i move all files on a web server this will work as a charm Thanks all for your support, this thread can be closed now

最后我想出了什么问题。我在系统本地文件上的 Google Chrome 上尝试了上面的代码。由于谷歌浏览器的安全设置,这个用例是不可能的。如果我移动网络服务器上的所有文件,这将是一种魅力感谢大家的支持,现在可以关闭此线程

回答by Andy E

Use contentWindow.documentinstead of document:

使用contentWindow.document代替document

var frame = window.parent.frames[0].contentWindow;
alert(frame.document.getElementById('textToSearch').value);

You can also just use contentDocumentfor most browsers, but not Internet Explorer 7 or older.

您也可以仅contentDocument用于大多数浏览器,但不适用于 Internet Explorer 7 或更早版本。