问题描述
这个问题几乎总结了一下。我正在尝试执行与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
.
尝试访问框架内容的脚本受制于同源策略,并且无法访问其他窗口对象中的大多数属性如果它是从其他域加载的。
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.
这篇关于如何检查iframe中的文本字段是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!