2010-06-30 109 views
3

我有下面的代碼,我有玩: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> 

基本上它的陣列中的一個程序來循環網址到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。

+0

首先,如果你正在使用IE6作爲主開發環境,你是一個受虐狂。其次,爲什麼你需要訪問parent.document?這是一個iframe改變頁面上另一個iframe的來源嗎? – 2010-06-30 01:44:00

+0

嗨,不得不使用IE6作爲我的工作場所使用它。其次,iframe1是我的主要內容,因爲它在其他地方也是如此。 – tonyf 2010-06-30 01:50:06

回答

1

此代碼更改應該工作。你需要給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> 
0

您是否嘗試過使用'top'而不是'parent'?

top.document.getElementById("iframe1").src = dashboard.url; 

http://www.w3schools.com/jsref/prop_win_top.asp

你有絕對的參考,不擔心哪個窗口,它實際上是這樣的。

+0

W3Schools? [W3Fools(http://w3fools.com/)! – Gareth 2011-05-09 16:28:29