<!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>
浏览器火狐,打开就关不了
把mywin提取到方法外面,否则在a2()里面是访问不到a1()的mywin的
我看了你的代码 提取到外面不好使呀
<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>