添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
天涯  ·  jquery - How do I ...·  1 年前    · 
天涯  ·  javascript - How do I ...·  1 年前    · 
天涯  ·  Jquery removeClass ...·  1 年前    · 
天涯  ·  jQuery how to find an ...·  1 年前    · 
想旅行的野马  ·  Android ...·  9 月前    · 
风流倜傥的葡萄酒  ·  html5 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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

After few steps I remove the class myClass, using

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

But the style position:absolute is not removing.

Any help will be much appreciated.

Because those 2 are not associated... teh class is used as a selector to find the elements after that whatever you do is irrespective of the class – Arun P Johny May 27, 2015 at 10:18 @ajai $('.myClass').css("position","absolute"); means you are directly injecting styles to the div not to the class – Optimus May 27, 2015 at 10:18 How can I attach styles to that class? Because I want to remove those styles after few steps. – Ajai May 27, 2015 at 10:19 I think this is a bad approach because he may remove other inline styles that were originally in the HTML – AmmarCSE May 27, 2015 at 10:34

Calling addClass will add a named style from a style-sheet.

Calling css will apply the styles inline to the element.

To remove the style you applied with css, simply do

$('.myClass').css("position","");
                if he has only one style position. $('.myClass').css("position",""); is simple , else it will be lengthy coding. Best to use jquerys default function of remove attr : removeAttr()
– Abin Abraham
                May 27, 2015 at 10:38

You can't do that using your current code the better way is create a new css class like this

.position-absolute{
 

the add this class to the div

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

and remove the class after use

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

That means you are selecting a div having Class named "myClass" and adding that style to that particular div.

But when you are writing $('.myDiv').removeClass('myClass');

That means you are selecting a div having Class named "myDiv" and removing that class named "myClass". But that class does not include that style("position","absolute"), so it will not be removed.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.