javascript 弹出窗口设置焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11397246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:02:41  来源:igfitidea点击:

Popup Window Setting Focus

javascriptfunctionpopupwindow

提问by John Doe

I have a popup window called myWindow and when it pops up i want the focus to be on the original window, not the popup. I took out the line myWindow.focus();and it still focused on the popup. Does anyone know how to keep the focus on the original window when the popup is created? Live Long and Prosper.

我有一个名为 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>

采纳答案by jeff

Just call window.focus()immediately after opening the popup, either in the function or right after it:

window.focus()打开弹出窗口后立即调用,无论是在函数中还是在它之后:

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

or

或者

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

回答by Elliot Bonneville

Call window.focus()instead of myWindow.focus().

调用window.focus()而不是myWindow.focus().



If that's not working, try blurring the window and then re-focusing it:

如果这不起作用,请尝试模糊窗口,然后重新聚焦:

window.blur();
window.focus();

回答by Sarjith Pullithodi

I was facing the same issue. I found a workaround.

我面临同样的问题。我找到了一个解决方法。

I modified like this:

我是这样修改的:

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

It worked for me in FF and Chrome.

它在 FF 和 Chrome 中对我有用。

回答by RandallB

In modern browsers it seems impossible to focus the original window.

在现代浏览器中,似乎不可能聚焦原始窗口。