如何检查iframe中的文本字段是否为空? [英] How do I check if a textfield in iframe is empty?

查看:116
本文介绍了如何检查iframe中的文本字段是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题几乎总结了一下。我正在尝试执行与Gmail类似的操作,如果您在文本字段中输入了内容,则网站会提示您确认是否要离开该页面。

Pretty much the question sums it up. I'm trying to do something similar to Gmail, where if you've entered stuff in a text field then the website will prompt you to to confirm if you want to leave the page.

但是,如果相关的文字字段位于iframe中会怎样?显然我可以为iframe分配一个id,但是有没有javascript命令或者可以检查iframe中的字段是否为空的东西?这是否可能?

But what if the text field in question is in an iframe? Obviously I can assign the iframe an id, but is there any javascript command or something that can check if a field in an iframe is empty or not? Is this even possible?

修改:嵌入iframe的iframe源和页面来自同一个域。

The iframe source and page the iframe is embedded in are from the same domain.

推荐答案

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

是的,有可能,你只需要访问< ; 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

这篇关于如何检查iframe中的文本字段是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆