4-7编程练习

来源:4-7 编程练习

MOCKINGT

2018-08-11 00:30:49

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>location对象</title>
    </head>
    <body>
        <input type="button" value="打开页面" id="open">
        
    </body>
    <script type="text/javascript">
           //补充代码
           function a(){
              var c = false;
              if(c=confirm('imooc') == true)
                open('www.imooc.com','imooc','width = 300, height = 300'); 
           }
           document.getElementById('open').addEventListener('click',a());
    </script>
</html>

这样写就会直接执行函数,而如果如下写的话就会成功

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>location对象</title>
    </head>
    <body>
        <input type="button" value="打开页面" id="open">
        
    </body>
    <script type="text/javascript">
           //补充代码
           document.getElementById('open').addEventListener('click',function(){
              var c = false;
              if(c=confirm('imooc') == true)
                open('www.imooc.com','imooc','width = 300, height = 300'); 
           });
    </script>
</html>


写回答

2回答

好帮手慕阿莹

2018-08-12

如果不写在函数中,在页面加载时,就会被加载执行了。

如果我的回答解决了你的问题,请采纳,祝学习愉快.

0

MOCKINGT

提问者

2018-08-11

我的问题是为什么函数会被先执行?

0

0 学习 · 10204 问题

查看课程