且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查iframe中的文本字段是否为空?

更新时间:2022-04-18 06:47:33

var value = document.getElementById('iframeID').contentDocument
                    .getElementById('theTextBoxId').value;

是的,有可能,你只需要访问&lt ; iframe> 的文档contentDocument

And yes, it's possible, you just need to get access to the <iframe> document with contentDocument.


来自DOM iframe元素,脚本可以通过contentWindow属性访问包含的HTML页面的窗口对象。 contentDocument属性引用iframe中的document元素(这相当于contentWindow.document),但IE8之前的Internet Explorer版本不支持。

From the DOM iframe element, scripts can get access to the window object of the included HTML page via the contentWindow property. The contentDocument property refers to the document element inside the iframe (this is equivalent to contentWindow.document), but is not supported by Internet Explorer versions before IE8.

尝试访问框架内容的脚本受制于同源策略,并且无法访问其他窗口对象中的大多数属性如果它是从其他域加载的。

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain.

MDN