一: html 中添加 onclick
<button id="vv" onclick="myfunction()"> 哈哈 </button>
二: JS 中定义函数绑定事件!
- var funcc = function () {
- alert('我爱编程')
- }
- var aa = document.getElementById('vv')
- aa.onclick = funcc
三: 直接定义函数与内容
- document.getElementById('vv').onclick = function () {
- alert('hasssshhaa')
- }
四: 利用 addEventListener
- document.getElementById('vv').addEventListener('click',funcc);
- // 或者:
- document.getElementById('vv').addEventListener('click',function () {
- alert('hahah')
- })
总结
来源: https://www.jb51.net/article/149588.htm