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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM