Javascript 代码失去焦点
我正在使用以下代码:
<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。
I have the following code that I am playing with:
<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>
Basically it's a routine to cycle through urls in an array into an iframe. My problem occurs when I set parent.document.getElementById("iframe1").src
to a url; it works for the first but it doesn't seem to cycle through to the next.
However, if I create an iframe in the same context of this javascript, say iframe2 and instead just use:
document.getElementById("iframe2").src = dashboard.url;
without the parent.document
call, all works fine.
Is it losing the focus of the javascript when I issue the parent.document
call?
Any ideas on how to bring focus back to this javascript code when calling a parent.document
?
I am using ie6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此代码更改应该有效。你需要给 iframe 一个名称,其次,我没有在 IE6 中测试它,但可以在 IE7 中工作。
This code change should work. You need to give iframe a name and secondly, I didnt test it in IE6, but works in IE7.
您是否尝试过使用“top”而不是“parent”?
http://www.w3schools.com/jsref/prop_win_top.asp
这样你有绝对的引用,不必担心它实际位于哪个窗口。
Have you tried using 'top' instead of 'parent'?
http://www.w3schools.com/jsref/prop_win_top.asp
That way you have an absolute reference and don't have to worry about which window it actually is in.