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

标签 javascript iframe textfield

这个问题几乎概括了这一点。我正在尝试做一些类似于 Gmail 的事情,如果您在文本字段中输入了内容,那么网站会提示您确认是否要离开该页面。

但是如果相关文本字段位于 iframe 中怎么办?显然我可以为 iframe 分配一个 id,但是有没有任何 javascript 命令或其他命令可以检查 iframe 中的字段是否为空?这可能吗?

编辑: iframe 源和嵌入 iframe 的页面来自同一域。

最佳答案

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

是的,这是可能的,您只需要访问 <iframe>文档包含 contentDocument .

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

关于javascript - 如何检查 iframe 中的文本字段是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14330132/

相关文章:

javascript - 使用 jquery 替换复选框列表中的标签

javascript - 工具提示在我的 div 之外

iframe - 如何在我的扩展程序中使用iframe嵌入YouTube channel 页面?

在 Windows 上禁用 Java AWT TextField 时不会出现幻影

javascript - 比较 NodeJs 和 Express 中的两个 JSON

javascript - 为什么无限对象不会破坏 javascript 中的流程?

html - 在 iframe 中隐藏图像

c# - 如何在 geckofx 中获取 iframe 地址?

javascript - 在 Oracle APEX 中使用自动完成功能将下拉列表添加到文本字段

flutter - 如何使用 flutter 小部件实现虚线文本字段