Jquery:在悬停时输入BGcolor,然后在mouseout时返回,但如果输入有焦点,则禁用mouseout BGcolor更改

and*_*ick 1 html jquery javascript-events

我有一个输入,当somone在输入上盘旋时我希望它的BGcolor变为灰色,然后当它们悬停在输入上时我希望它的BGcolor变回白色(默认颜色).但是我也希望输入的BGcolor在焦点输入时变为灰色,但是当人们专注于输入,然后将光标从输入移开时,即使输入有BGcolor,BGcolor也会变回白色焦点.因此,基本上我希望当输入框具有焦点时禁用鼠标悬停功能,然后当它不再具有焦点时,重新启用鼠标悬停功能.

谁能帮我吗?我不知道怎么做...

Nic*_*ver 7

你可以只用 CSS 做这个(为什么不保持简单?),根本不需要JavaScript:

input { background: white; }
input:focus, input:hover { background: gray; }
Run Code Online (Sandbox Code Playgroud)

你可以在这里测试一下.

如果我完全按照你说的那样,它会在脚本中看起来像这样(为了完全禁用悬停),你可以使用.live()或者.delegate()像这样:

$("input").live("focus blur", function() {
  $(this).toggleClass("focused");
});
$("input:not(.focused)").live("mouseenter", function() {
  $(this).addClass("grayBG");
}).live("mouseleave", function() {
  $(this).removeClass("grayBG");
});
Run Code Online (Sandbox Code Playgroud)

最后2个处理程序在它聚焦时(并且具有.focused类)不会对元素起作用.