1、在标签里直接绑定
<input type="button" value="点我呦" onclick="alert("hello world!")"/><!--或者--><input type="button" value="点我呦" onclick="testAlert()"><script type="text/javascript"> function testAlert(){ alert("hello world!"); }</script>
2、在Javascript里绑定
<input type="button" value="点我呦" id="demo"><script type="text/javascript"> document.getElementById("demo").onclick=function testAlert(){ alert("hello world!"); }</script>
3、通过事件监听函数绑定
<script><button id="btn2">点我2</button>var $btn2 = document.querySelector('#btn2'); // 参数1:事件名称 参数2:监听器(监听函数) 参数3:事件模式 $btn2.addEventListener('click', function () { alert('dom事件二次绑定') }, false) // 函数也可以在外边声明 $btn2.addEventListener('click', text, false) function text() { alert('在绑定一次') }</script>