JQuery.addClass not working

This looks correct, but does not work. I would like the "huh" div to become opaque when the menu freezes. I tried this with fadein / out and it worked, but only once, which was strange.

<script type="text/javascript"> $( function() { $('#menuNav').hover( function() { $('#huh').addClass('.opacity'); }, function(){ $('#huh').removeClass('.opacity'); }); }); </script> .opacity { opacity: 0.3; } 
+6
source share
8 answers

Use it without a dot:

  $(function(){ $('#menuNav').hover(function(){ $('#huh').addClass('opacity'); }, function(){ $('#huh').removeClass('opacity'); }); }); 
+24
source

.hover() does a lot of events, it is better to use .mouseenter() . Note also that when you add a class, you do not . (point).

 $(function(){ $('#menuNav').mouseenter(function(){ $('#huh').addClass('opacity'); }, function(){ $('#huh').removeClass('opacity'); }); }); 
+5
source

Used to delete .

  $('#huh').addClass('opacity'); // remove . $('#huh').removeClass('opacity'); // remove . 

===============

Or used to

 toggleClass in jquery 

 $(function(){ $('#menuNav').hover(function(){ $('#huh').toggleClass('opacity'); }); }); 

More on this

+4
source
 $( function() { $('#menuNav').hover( function() { $('#huh').toggleClass('opacity'); }); }); 
+4
source

try it

  <script> $(function(){ $('#menuNav').hover(function(){ $('#huh').addClass('opacity'); }, function(){ $('#huh').removeClass('opacity'); }); }); </script> 
+2
source

You have one . in the name of your class in addClass and removeClass , you need to add and remove without a dot when calling these methods. i.e.

 $(function() { $('#menuNav').hover(function(){ $('#huh').addClass('opacity'); }, function(){ $('#huh').removeClass('opacity'); }); }); 
+2
source

. delete and then use this javascript

  $(function(){ $('#menuNav').hover(function(){ $('#huh').addClass('opacity'); }, function(){ $('#huh').removeClass('opacity'); }); }); 
+1
source

try it

 $("#menuNav").mouseenter(function() { $('#huh').addClass('opacity'); }).mouseleave(function() { $('#huh').removeClass('opacity'); }); 
+1
source

Source: https://habr.com/ru/post/950601/


All Articles