Jquery removeClass does not remove related styles

I have a div with some class and I add some css using jQuery

$('.myClass').css("position","absolute"); 

After a few steps, I myClass class using

 $('.myDiv').removeClass('myClass'); 

But the position:absolute style is not removed.

Any help would be greatly appreciated.

+6
source share
5 answers

Use removeAttr( 'style' ) .

Which jquery function removes the element.style element.

+4
source

This is because when you did

 $('.myClass').css("position","absolute"); 

he added a style inline

A call to addClass will add a style style with a name from the stylesheet.

A css call will apply inline styles to the element.

To remove the style you applied with css , just do

 $('.myClass').css("position",""); 
+1
source

position:absolute not bound to a class

When we delete a class, it removes the style defined with it, like a style definition in CSS files

0
source

You cannot do this using your current code, the best way is to create a new css class like this

 .position-absolute{ position:absolute; } 

add this class to div

 $('.myDiv').addClass('position-absolute'); 

and delete the class after use

0
source

When you write $ (". MyClass') CSS (" position "," absolute ").

This means that you select a div with the class "myClass" and add this style to this particular div.

But when you write $ ("MyDiv.) RemoveClass ('MyClass') ;.

This means that you select a div with the class "myDiv" and delete this class with the name "myClass". But this class does not include this style ("position", "absolute"), so it will not be deleted.

0
source

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


All Articles