javascript - 防止blur()运行

标签 javascript jquery

我有一个引导弹出窗口,它在输入的焦点上变为事件状态。弹出窗口中包含一个用于执行 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                                   
});

最佳答案

你看过stopImmediatePropagation吗? ?如果使用 jQuery,则事件处理程序将按照它们绑定(bind)的顺序触发。只需在模糊之前绑定(bind)点击,然后在点击事件中运行 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  
});

关于javascript - 防止blur()运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31061523/

相关文章:

javascript - Vue js vuelidate 自定义验证

javascript - 如何将参数传递给回调

javascript - javascript 原型(prototype)继承如何用于数组、函数

javascript - 使用 JQuery onblur 设置文本框值

jquery - 在 WordPress 中使用 fancybox 加载内联内容

javascript - 使用 lazysizes 延迟加载背景图像

javascript - 如何在 Ember CLI 结构中的 EmberJS 主页上设置 “Loading Data from a Server” 示例?

javascript - 如何在保持所需物体大小的透视相机和正交相机之间切换

jquery - 如何在center li上添加事件课

javascript - 在不更改子元素的情况下更改元素的文本