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