转自链接:https://www.shuzhiduo.com/A/xl560MKrzr/

在jQuery的事件绑定中,如果元素同时绑定了单击事件(click)和双击事件(dblclick),那么执行单击事件(click)时,不会触发双击事件(dblclick), 执行双击事件(dblclick)时却会触发两次单击事件(click)。

html代码:

<button>点击</button>

JQ代码:

<script>
        $(function () {
            // 编写相关jQuery代码
            // 单双击的时间间隔是300ms
            // 先做两次单击 一次双击 中间间隔 小于300ms
            var timer = null;
 
            // 获取事件源,绑定事件
            $('button').click(function () {
                // 清除定时器
                clearTimeout(timer);
                // 设置定时器 300ms 一次性定时器
                timer = setTimeout(function () {
                    console.log('单机');
                },300)
            });
 
            $('button').dblclick(function () {
                // 清除定时器
                clearTimeout(timer);
                console.log('双击');
            })
        })
    </script>

 

相关文章:

  • 2021-09-08
  • 2022-12-23
  • 2021-07-11
  • 2021-08-29
  • 2022-12-23
  • 2022-03-04
  • 2021-08-12
  • 2021-06-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-16
  • 2022-02-07
  • 2021-05-03
相关资源
相似解决方案