Javascript Jquery addClass 和 Remove Class 悬停
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10706667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery addClass and Remove Class on hover
提问by user1405690
Okay i would like to add a class cfse_a
to an element #searchput
when the mouse is hovering over the element and then when the mouse is not hovering over the element then remove class cfse_a
.
好的,当鼠标悬停在cfse_a
元素上#searchput
时,我想向元素添加一个类,然后当鼠标未悬停在元素上时,然后删除类cfse_a
。
回答by VisioN
Use hover
event with addClass
and removeClass
methods:
使用hover
事件addClass
和removeClass
方法:
$("#searchput").hover(function() {
$(this).addClass("cfse_a");
}, function() {
$(this).removeClass("cfse_a");
});
回答by thecodeparadox
$('#searchput').hover(function() {
$(this).addClass('cfse_a'); // add class when mouseover happen
}, function() {
$(this).removeClass('cfse_a'); // remove class when mouseout happen
});
You can also use:
您还可以使用:
$('#searchput').hover(function() {
$(this).toggleClass('cfse_a');
});
see toggleClass()
回答by Kaidul
$("#searchput").hover(function() {
$(this).addClass("cfse_a");
}, function() {
$(this).removeClass("cfse_a");
});
Use it.hope it help !
使用它。希望它有帮助!
回答by ssj1980
Hope this helps.
希望这可以帮助。
$('#searchput').mouseover(function() {
$(this).addClass('cfse_a');
}).mouseout(function(){
$(this).removeClass('cfse_a');
});