如何区分单击事件和双击事件?

我有一个带有id的li按钮"my_id"。我two jquery用这个元素附加了 事件


1。


$("#my_id").click(function() { 

    alert('single click');

});

2。


$("#my_id").dblclick(function() {

    alert('double click');

});

但每次它都给了我 single click


郎朗坤
浏览 970回答 3
3回答

慕娘9325324

一个简单的功能。不需要jquery或其他框架。将您的函数作为参数传递<div onclick="doubleclick(this, function(){alert('single')}, function(){alert('double')})">click me</div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; function doubleclick(el, onsingle, ondouble) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (el.getAttribute("data-dblclick") == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el.setAttribute("data-dblclick", 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (el.getAttribute("data-dblclick") == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onsingle();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el.removeAttribute("data-dblclick");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 300);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el.removeAttribute("data-dblclick");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ondouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP