为了账号安全,请及时绑定邮箱和手机立即绑定

请问这段代码错在哪里了

<!DOCTYPE html>

<html>

 <head>

  <title> new document </title>  

  <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>   

  <script type="text/javascript">  

    function openWindow()

    {

     var open=confirm("是否打开新窗口?");

     if(open==true){

         var url=prompt("打开新窗口","http://www.imooc.com/");

         if(url=!null){

             window.open(url,"_blank",'width=400px,height=500px,menubar=no,toolbar=no');

         }

     }

      else

   {alert("结束");}

   

   }

    //打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏。

  else

{alert("结束");}

  

}

    

    

  </script> 

 </head> 

 <body> 

 <input type="button" value="新窗口打开网站" onclick="openWindow()" /> 

 </body>

</html>


正在回答

3 回答

function openWindow() {
    var open = confirm("是否打开新窗口?");
    //你这里的 ( 是中文的
    if (open == true) {
        //这里url是true 或 false 不是你输入的地址
        var url = prompt("打开新窗口", "http://www.imooc.com/");

        if (url) {
            window.open("http://www.imooc.com/", "_blank", 'width=400px,height=500px,menubar=no,toolbar=no');
        //} 这里多了一个 }
        }
        else {
            alert("结束");
        }
    }
    //打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏。
    else {
        alert("结束");
    }

}

对着注释看

0 回复 有任何疑惑可以回复我~
#1

汤勺614614 提问者

非常感谢!
2015-11-25 回复 有任何疑惑可以回复我~
if(url=!null){

这里应该是!=,而不是=!。

!=是不等于,=!是取反后赋值。

1 回复 有任何疑惑可以回复我~
if(open==true){

这里的(敲成中文的,改成英文的。

花括号嵌套错乱。括号是成对出现的,删去第一个else前的一个}即可。

修改后的全代码

<!DOCTYPE html>
<html>
<head>
    <title> new document </title>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
    <script type="text/javascript">
        function openWindow()
        {
            var open=confirm("是否打开新窗口?");
            if(open==true){
                var url=prompt("打开新窗口","http://www.imooc.com/");
                if(url=!null){
                    window.open(url,"_blank",'width=400px,height=500px,menubar=no,toolbar=no');
                }else{
                    alert("结束");
                }
            }else{
                alert("结束");}

        }
    </script>
</head>
<body>
<input type="button" value="新窗口打开网站" onclick="openWindow()" />
</body>
</html>


1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

请问这段代码错在哪里了

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号