JS调用函数的两种方式

<script type="text/javascript">
    window.onload = init; //onload 表示页面全部加载完毕后,再调用init()
    function init() {
        // 第一种调用函数的方式
        window.document.getElementById('btn1').onclick = test; //直接使用document获取到ID对象的属性onclick,将其赋值为一个函数名,点击鼠标即可调用test函数
        //第二种调用函数的方式。匿名调用
        window.document.getElementById('btn2').onclick = function (){
            alert("you've hit button 2 already!");
        }
    }
    function test(){
        alert("you've hit button 1 already!");
    }
</script>
</head>
<body>
    <input type="button" value ="hit me " id = "btn1">
    <input type="button" value ="hit me " id = "btn2">
</body>

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript">
        var score = prompt("please input a number:"); //score接收用户的输入,返回String
        var score = +prompt("please input a number:"); //+ 把string 转换成number类型
        outer:
            for(var i=0 ; i<5 ; i++){
                console.log("@外层循环"+i)
                for(var j=0 ; j<5; j++){
                    break outer;
                    console.log("内层循环:"+j);
                }
            }
        console.time("计时器的名字");
            代码区
        console.timeEnd("计时器的名字");


      </script>
</head>
<body>
</body>
</html>

 

posted @ 2019-06-17 09:36  Coding_Changes_LIfe  阅读(3101)  评论(0编辑  收藏  举报