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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:35:49  来源:igfitidea点击:

Jquery addClass and Remove Class on hover

javascriptjqueryfile

提问by user1405690

Okay i would like to add a class cfse_ato an element #searchputwhen 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 hoverevent with addClassand removeClassmethods:

使用hover事件addClassremoveClass方法:

$("#searchput").hover(function() {
    $(this).addClass("cfse_a");
}, function() {
    $(this).removeClass("cfse_a");
});

DEMO:http://jsfiddle.net/G23EA/

演示:http : //jsfiddle.net/G23EA/

回答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()

切换类()

DEMO

演示

回答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');
});