小兵以太网测试工具实战:二层打流与故障定位指南
2026/10/7 9:07:20
一.事件监听版本
版本一:
事件源.on事件 = function(){}<body> <button>点击</button> <script> const btn = document.querySelector('button'); btn.onclick = function(){ console.log('点击了按钮'); } </script> </body>版本二:
事件源.addEventListener(事件,事件处理函数)<body> <button>点击</button> <script> const btn = document.querySelector('button'); btn.addEventListener('click',function(){ console.log('点击了按钮'); }) </script> </body>区别:
on方式会被覆盖,addEventListener方式可绑定多次,拥有事件更多特性,推荐使用
二.鼠标事件
1.click:鼠标点击
2.mouseenter:鼠标经过
<style> div{ width: 200px; height: 200px; background-color: green; } </style> <body> <div></div> <script> const div = document.querySelector('div'); div.addEventListener('mouseenter