1.定義和使用
click:單擊事件
dblclick:雙擊事件
注意:區分單擊還是雙擊,使用延時定時器
代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js_excise</title> </head> <style> #box{ width: 100px; height: 100px; background-color: aquamarine; } </style> <body> <div id="box"></div> <script type="text/javascript"> //單機事件 var time var jsBox = document.getElementById('box') jsBox.addEventListener('click',function(){ //先清除一次 clearTimeout(time) //再執行計時器 time = window.setTimeout(function(){ console.log('單機') },250) },false) //雙擊事件 jsBox.addEventListener('dblclick',function(){ // 清除第二次單機計時器 clearTimeout(time) console.log('雙擊') },false) </script> </body> </html>
輸出:
程序運行原理: