【问题标题】:Prevent blur() from running防止 blur() 运行
【发布时间】:2015-06-25 21:59:11
【问题描述】:

我有一个引导弹出框,它在输入的focus 上变为活动状态。弹出框包含一个用于执行 ajax 请求的按钮。

我有一个blur 函数,如果用户点击离开输入框,它会关闭弹出框:

$(document).on('blur','.open-popover',function(){        
    $(".popover").attr("style","");
    //and do other things too  
});

如果用户按下弹出窗口中的按钮,我试图阻止上面的 blur 函数运行:

$(document).on('click','button',function(){
    //prevent the blur actions and 
    //do ajax stuff                                   
});

【问题讨论】:

标签: javascript jquery


【解决方案1】:

你看过stopImmediatePropagation吗?如果使用 jQuery,则事件处理程序按照绑定的顺序触发。只需在模糊之前绑定点击,然后在点击事件中运行 stopImmediatePropagation。

// bind click event first
$(document).on('click','button',function(event){
    // keeps the rest of the handlers from being executed and prevents the event from bubbling up the DOM tree.
    event.stopImmediatePropagation();                            
});

// then attach blur
$(document).on('blur','.open-popover',function(){        
    $(".popover").attr("style","");
    //and do other things too  
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2011-08-25
    • 2014-11-08
    • 2011-03-12
    相关资源
    最近更新 更多