最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery addClass and Remove Class on hover - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited May 22, 2012 at 17:09 VisioN 145k34 gold badges286 silver badges289 bronze badges asked May 22, 2012 at 17:03 user1405690user1405690 1492 gold badges2 silver badges9 bronze badges 1
  • api.jquery.com/hover, api.jquery.com/addClass and api.jquery.com/removeClass. – Guffa Commented May 22, 2012 at 17:06
Add a comment  | 

4 Answers 4

Reset to default 20

Use hover event with addClass and removeClass methods:

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

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

$('#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

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

Use it.hope it help !

Hope this helps.

$('#searchput').mouseover(function() {
    $(this).addClass('cfse_a');
}).mouseout(function(){
    $(this).removeClass('cfse_a');
});
发布评论

评论列表(0)

  1. 暂无评论