function show(){ console.log('show'); } function print(){ console.log('print'); } <button onclick="show()" id="btn1" onclick="print()">html标签事件绑定</button> //触发的方法只有show方法 <button onclick="show();print()" id="btn1">html标签事件绑定</button> //一个事件,触发两个方法
<button id="btn2">js事件绑定</button> document.getElementById("btn2").onclick = show; document.getElementById("btn2").onclick = print; //只能触发print方法,若是须要同时触发两个方法,只能使用事件监听
<button id="btn3">事件监听</button> //show和print两个方法均可以触发 document.getElementById("btn3").addEventListener("click",show); document.getElementById("btn3").addEventListener("click",print); //移除事件监听 document.getElementById("btn3").removeEventListener("click");