我有一个脚本在可能嵌套在 iframe 中的文档中运行,也可能不嵌套在 iframe 中。我已经解决了检测嵌套的问题,但是我无法弄清楚当它是 iframe 时如何检测 DOM 的就绪状态。
这是我已经拥有的:
if (window.self !== window.top) {
// is nested
// DOM ready test here
// execute code here
} else {
// is not nested
$(document).ready(function() {
// execute code here
});
}
我已经阅读了这篇文章,但我没有看到我的具体问题的答案。我可能会误解那里的解决方案,所以请随时纠正我。
编辑:这似乎是一个 Fancybox 问题。这段代码:
alert(document.getElementById('username').className);
document.getElementById('username').focus();
我看到一个具有正确值的警报,但表单元素没有获得焦点。这告诉我检测 DOM 的就绪状态不是问题。我尝试使用的原始脚本是这样的:
$(document).ready(function() {
$('input.focus:last').focus();
});
编辑 2/解决方案:我不得不求助于 Fancybox 特定的解决方案,因为它似乎是 Fancybox 特定的问题。我将此行添加到这些 Fancybox iframed 表单的配置中:
'onComplete':function(){$('input.focus:last').focus();}