我正在使用以下代码:
<script type="text/javascript">
var Dash = {
nextIndex: 0,
dashboards: [
{url: 'http://www.google.com', time: 5},
{url: 'http://www.yahoo.com', time: 10}
],
display: function()
{
var dashboard = Dash.dashboards[Dash.nextIndex];
parent.document.getElementById("iframe1").src = dashboard.url;
Dash.nextIndex = (Dash.nextIndex + 1) % Dash.dashboards.length;
setTimeout(Dash.display, dashboard.time * 1000);
}
};
window.onload = Dash.display;
</script>
基本上,循环访问数组中的 url 到 iframe 是一个例程。当我将 parent.document.getElementById("iframe1").src
设置为 url 时,我的问题就出现了;它适用于第一个,但似乎没有循环到下一个。
但是,如果我在这个 javascript 的相同上下文中创建一个 iframe,请说 iframe2 而不是只使用:
document.getElementById("iframe2").src = dashboard.url;
没有 parent.document
调用,一切正常。
当我发出 parent.document
调用时,它是否失去了 javascript 的焦点?
关于在调用 parent.document
时如何将焦点带回此 javascript 代码的任何想法?
我正在使用 ie6。
最佳答案
此代码更改应该有效。你需要给 iframe 一个名字,其次,我没有在 IE6 中测试它,但在 IE7 中工作。
<script type="text/javascript">
var Dash = {
nextIndex: 0,
dashboards: [
{url: 'http://www.rediff.com', time: 5},
{url: 'http://www.google.com', time: 10}
],
display: function()
{
var dashboard = Dash.dashboards[Dash.nextIndex];
parent.frames["fname"].location.href = dashboard.url;
window.focus();
Dash.nextIndex = (Dash.nextIndex + 1) % Dash.dashboards.length;
setTimeout(Dash.display, dashboard.time * 1000);
}
};
window.onload = Dash.display;
</script>
关于Javascript 代码失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3145914/