Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在其中parent.html设置了一个 iframe child.html。
parent.html
child.html
在子框架中,我写了js:
$(function () { var child = 6; })
在父框架中,我写js:
$(function () { alert(window.frames[0].child); });
但警报结果是“未定义”?
如何使用 jquery 正确引用另一个框架的变量?
你看到undefined是因为你将它作为一个局部变量(仅限于该document.ready处理程序),你需要在子框架中使用它来获得全局:
undefined
document.ready
$(function () { window.child = 6; });
此外,不能保证document.ready父框架上的 a 不会在子框架之前执行(在每个框架中粘贴一个alert()以查看顺序)......事实上它应该是相反的。如果您不立即使用它,那不是问题……如果您需要稍后执行它。
alert()