JQuery 忽略焦点上的 mouseleave

标签 jquery focus mouseleave

抱歉,如果这看起来很基础,我是 JQuery 新手。

我的页面上有一个密码元素,我希望它能够在该框处于事件状态并且在框中键入时保持绿色,但 mouseleave 似乎优先于焦点。

这是该元素的代码:

$(":password").on({
mouseenter: function(){
    $(this).css("background-color", "lightblue");
}, 

blur: function(){
    $(this).css("background-color", "white");
},
focus: function(){

    $(this).css("background-color", "#a6ff4d");

},
mouseleave: function(){
    if ($(":password").not(":focus")) {
    $(this).css("background-color", "white");
    };
},

click: function(){
    $(this).css("background-color", "chartreuse");
    } 
}); 

我已经尝试过:

mouseleave: function(){
if ($(":password").not(":focus")) {
$(this).css("background-color", "white");
};
},

mouseleave: function(){
if ($("this").not(":focus")) {
$(this).css("background-color", "white");
};
},

这是最新的jquery。

抱歉,我已经搜索过,但我在这里找到的答案似乎并没有解决我的问题。

最佳答案

您可以使用jquery .is()检查密码字段是否打开的方法:focus 。因此,将 onleave 事件中的代码部分替换为:

if (!$(this).is(":focus")) {
    $(this).css("background-color", "white");
}

这会对你有帮助

关于JQuery 忽略焦点上的 mouseleave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266799/

相关文章:

javascript - ajax json 添加尾随冒号

javascript - 如何在 Odoo 中聚焦一个 TransientModel 形式的字段?

javascript - 拖动时 jQuery mouseleave 未正确触发

jquery - Mouseenter/Mouseleave 简化 jQuery

jquery - 如何循环播放 jquery.cycle() 幻灯片两次

javascript - CORS 在 jquery 中工作正常但在 angularjs 中不工作

javascript - 如何同步包含克隆元素的 jquery 可排序列表?

Java 通过自定义 JCheckBoxList 使用制表键进行导航

windows - 聚焦表单上的点击丢失

c# - Windows 窗体上的鼠标离开事件