弹出窗口设置焦点

Popup Window Setting Focus

本文关键字:焦点 设置 窗口      更新时间:2023-09-26

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

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


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

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

打开弹出窗口后立即调用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();
}

我也面临同样的问题。我找到了一个变通办法。

我这样修改:

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

它在FF和Chrome中对我有效。

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