1. 程式人生 > >JAVAscript學習筆記 js句柄監聽事件 第四節 (原創) 參考js使用表

JAVAscript學習筆記 js句柄監聽事件 第四節 (原創) 參考js使用表

必須 htm -c eve property blog uncaught event add

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>句柄添加監聽事件</title>
    <script type="text/javascript" src="tzy.js"></script>
</head>
<body>
<button id="mybtn" onclick="demo()">按鈕</button>
<script>
    /*
//註意這種引用樣式必須放在button後面不然出現 //Uncaught TypeError: Cannot read property ‘addEventListener‘ of null //錯誤:頁面未加載完成 var x = document.getElementById("mybtn"); x.addEventListener("click", hello); x.addEventListener("click", world);//添加 x.removeEventListener("click", world);//移除 function hello() { alert("hello"); } function world() { alert("world"); }
*/ </script> </body> </html>
//註意這種引用樣式必須放在button後面不然出現
//Uncaught TypeError: Cannot read property ‘addEventListener‘ of null
//錯誤:頁面未加載完成
function demo(){
    var x = document.getElementById("mybtn");

    x.addEventListener("click", hello);
    x.addEventListener("click", world);//添加
    x.removeEventListener("click", world);//移除
}
function hello() {
    alert("hello");
}

function world() {
    alert("world");
}

JAVAscript學習筆記 js句柄監聽事件 第四節 (原創) 參考js使用表