如何使用javascript将焦点返回到父窗口? [英] How to return focus to the parent window using javascript?

查看:92
本文介绍了如何使用javascript将焦点返回到父窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不能将焦点返回到Firefox 4和5中的父窗口

This doesn't work as to return the focus to the parent window in Firefox 4 and 5

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

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

</body>
</html> 

如何使用javascript将焦点返回到父窗口?

How can I return focus to the parent window using javascript?

推荐答案

您需要为父窗口命名。

Parent.html

Parent.html

<a id="link" href="#">Click to child.html </a>
<script type="text/javascript">
    $(document).ready(function () {
        window.name = "parent";
        $('#link').click(function (event){ 
            event.preventDefault();
            window.open("child.html");
        });
    });
</script>

Child.html

Child.html

<a id="link" href="#">Return to Parent </a>
<script type="text/javascript">
    $(document).ready(function () {
        $('#link').click(function(event) {
            event.preventDefault();
            var goBack = window.open('', 'parent');
            goBack.focus();
        });
    });
</script>

现在,无论何时单击child.html中的链接,父窗口都将被聚焦。

Now, whenever you click the link in child.html, the parent window will be focused.

这篇关于如何使用javascript将焦点返回到父窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆