问答详情
源自:2-6 JavaScript-关闭窗口(window.close)

关不了呀!!

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>javascript</title>
    <script type="text/javascript">
            function context()
            {
                var mychar="I love U!"
            alert(mychar);
            }
              function a1(){
                 var mywin=window.open("http://www.imooc.com" ,'_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes');}
             function a2(){
                 mywin.close();
                 }
    </script> 
</head>
<body>
    <form>
     <input type="button" value="点击我" onclick="context()" />
     <br>
      <input type="button" onclick="a1()" value="打开窗口" />
    <input type="button" onclick="a2()" value="关闭窗口" />
   </form>
</body>
</html>

浏览器火狐,打开就关不了

提问者:clinna 2017-04-22 01:20

个回答

  • ziom
    2017-04-22 08:42:37
    已采纳

    把mywin提取到方法外面,否则在a2()里面是访问不到a1()的mywin的

  • 慕粉1254111012
    2017-06-12 00:01:55

    我看了你的代码  提取到外面不好使呀

    <html>
    <head>
        <meta charset="UTF-8">
        <title>javascript</title>
        <script type="text/javascript">
    var mywin=window.open("http://www.imooc.com" ,'_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes');
              
                function context()
                {
                    var mychar="I love U!"
                alert(mychar);
                }
                  function a1(){
                     mywin.open();
                        }
    
                 function a2(){
                     mywin.close();
                     }
        </script> 
    </head>
    <body>
        <form>
         <input type="button" value="点击我" onclick="context()" />
         <br>
          <input type="button" onclick="a1()" value="打开窗口" />
        <input type="button" onclick="a2()" value="关闭窗口" />
       </form>
    </body>
    </html>