【问题标题】:Jquery cursor drag and release outside input detected as clickJquery光标拖动和释放外部输入检测为点击
【发布时间】:2019-04-09 19:41:55
【问题描述】:

您好,当您在输入字段之外拖动和释放时,有没有办法防止 jquery 检测到单击事件? 问题是我有一个模式窗口,当您在窗口外(背景)单击时会关闭。 当您在模态窗口中有一个输入字段并选择输入内的文本并将鼠标一直拖出模态窗口区域时,然后释放单击,模态将关闭,因为它检测到该元素上的单击事件当它实际上不是点击事件而是“释放”事件时。

这是一个例子https://jsfiddle.net/imurphy/tafwzero/1/

点击事件代码:

$('.how').on('click',function(e){
    e.preventDefault();

  if(!$(e.target).is('.how')){
                return;
            }
    alert('Modal Closed');
});

谢谢

【问题讨论】:

    标签: javascript jquery events click


    【解决方案1】:

    检查下面的代码。您需要使用 mousedown 和 mouseup 处理拖放

    var isDragging = false;
        $(".how")
        .mousedown(function() {
            $(window).mousemove(function() {
                isDragging = true;
                $(window).unbind("mousemove");
            });
        })
        .mouseup(function() {
            var wasDragging = isDragging;
            isDragging = false;
            $(window).unbind("mousemove");
            if (!wasDragging) {
                $(this).selection();
            }
        });
    
    
        $('.how').on('click',function(e){
        e.preventDefault();
    
      if(!$(e.target).is('.how')){
                    return;
                }
        alert('Modal Closed');
    });
    

    【讨论】:

    • 感谢您的帮助,但我仍然得到相同的结果
    • 你在使用 Bootstrap modal 吗?如果是,请使用静态背景 $('#myModal').modal({backdrop: 'static', keyboard: false})
    • sorry but no,我没有使用bootstrap的modal,我自己做了一个简单的modal
    • 然后将背景设为静态模式。
    • 对不起我不能,默认的动作是点击背景关闭模态框,直接点击背景需要关闭​​,没有释放点击的时候关闭
    猜你喜欢
    • 2014-09-29
    • 2020-02-13
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多