2012-07-09 167 views
4

我有一個名爲myWindow的彈出窗口,當它彈出時,我希望焦點位於原始窗口上,而不是彈出窗口。我拿出myWindow.focus();這一行,它仍然關注彈出窗口。有沒有人知道如何在創建彈出窗口時將注意力集中在原始窗口上?健康長壽·繁榮昌盛。彈出窗口設置焦點

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
function openWin() 
{ 
myWindow=window.open('','','width=200,height=100'); 
myWindow.document.write("<p>This is 'myWindow'</p>"); 
myWindow.focus(); 
} 
</script> 
</head> 
<body> 

<input type="button" value="Open window" onclick="openWin()" /> 

</body> 
</html> 
+2

+1「Live Long and Prosper」。 -1沒有綁定你的事件處理程序在JavaScript中。 – jbabey 2012-07-09 14:39:55

+0

查看http://stackoverflow.com/questions/7996252/window-popups-how-to-get-window-blur-or-window-focus-to-work-in-firefox-4 – j08691 2012-07-09 14:41:57

+0

我將不勝感激只是一個+ 1請,因爲我是新的這個... :) – 2012-07-09 14:49:46

回答

3

就叫window.focus()打開彈出後立即,無論是在功能還是之後它:

<input type="button" value="Open window" onclick="openWin(); window.focus();" /> 

function openWin() 
{ 
myWindow=window.open('','','width=200,height=100'); 
myWindow.document.write("<p>This is 'myWindow'</p>"); 
window.focus(); 
} 
+0

這將是更有意義的將它包括在函數中。 – sachleen 2012-07-09 14:41:32

+0

很好的建議,但仍然沒有工作... – 2012-07-09 14:42:44

+0

@sachleen - 如果他不想每次調用openWin()時都將焦點設置回主窗口,則不需要。但我編輯了我的答案,以兩種方式展示。 – jeff 2012-07-09 14:44:59

4

致電window.focus()而不是myWindow.focus()


如果這不是工作,嘗試模糊的窗口,然後重新聚焦它:

window.blur(); 
window.focus(); 
+0

好主意,但沒有奏效。即時開始認爲我想要做的事是不可能的... – 2012-07-09 14:40:21

+0

仍然沒有運氣... – 2012-07-09 14:44:20

+0

實際上它的工作原理,只是不在MZF – 2012-07-09 14:47:56

1

我正面臨同樣的問題。我找到了一個解決方法。

我修改這樣的:

myWindow.document.write("<p>This is 'myWindow'</p>"); 
myWindow.document.write("<script>window.opener.focus();</script>"); 

它在FF和Chrome爲我工作。

+0

或者像這樣重寫 'myWindow.document.write(''); – 2012-11-09 05:42:26