【问题标题】:dblclick not working as expecteddblclick 未按预期工作
【发布时间】:2023-03-11 23:25:01
【问题描述】:

我在这里有一些代码http://jsfiddle.net/ggHwH/,每次按下 UP 按钮时将框边框增加 1px,每次按下 DOWN 按钮时将边框减小 1px。现在我想感觉到双击向下按钮并立即将边框减小到 0。我遇到的问题是,如果您尝试一次单击一个 px 的边框,您可以即使您单击向下按钮的速度不超过每秒一次,也只能触发双击。看来我必须在不到 250 毫秒的时间内完成两次点击才能触发双击。有谁知道怎么回事?

谢谢。

   $('#up').click ( function() {
        $('#box').css({'border-top-width' : '+=1px', 'borderRightWidth' :    '+=1px','borderBottomWidth' : '+=1px','borderLeftWidth' : '+=1px'});
    });

$('#down').click ( function() {
    $('#box').css({'border-top-width' : '-=1px', 'borderRightWidth' : '-=1px','borderBottomWidth' : '-=1px','borderLeftWidth' : '-=1px'});
});

$('#down').dblclick ( function() {
    $('#box').css({'border-top-width' : '0px', 'borderRightWidth' : '0px','borderBottomWidth' : '0px','borderLeftWidth' : '0px'});
});

【问题讨论】:

标签: javascript jquery


【解决方案1】:

dblclickclick 混合在一起并不是一个好的做法。

但是,对于您的问题,您可以创建“自己的”dblclick。您只需要添加 2 个 var :

var isDbl = false;
var timer = null;

那么你的click函数会设置一个250ms的定时器并且有一个条件:

$('#down').click ( function() {
    clearTimeout(timer)
    timer = setTimeout(function(){
        isDbl = false
    }, 250)
    if(isDbl){
        $('#box').css({'border-top-width' : '0px', 'borderRightWidth' : '0px','borderBottomWidth' : '0px','borderLeftWidth' : '0px'});
        isDbl = false;
    }else{
        $('#box').css({'border-top-width' : '-=1px', 'borderRightWidth' : '-=1px','borderBottomWidth' : '-=1px','borderLeftWidth' : '-=1px'});
    isDbl = true
    }
});

小提琴:http://jsfiddle.net/ggHwH/4/

【讨论】:

  • 非常好的卡尔-安德烈!感谢您将该解决方案放在一起。也许我可以为你做点小事作为回报。我打开了您的网站,并在顶部注意到“Web 开发人员/设计师对每项新技术感兴趣”所有新技术”。再次感谢您的帮助。
猜你喜欢
  • 2021-06-04
  • 2022-01-24
  • 2015-05-11
  • 2020-05-15
  • 2014-10-31
  • 2018-02-12
  • 2014-01-20
  • 2015-01-13
  • 2013-08-01
相关资源
最近更新 更多