抱歉,如果这看起来很基础,我是 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/