jQuery 如何从jquery的所有元素中删除类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5997960/
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-26 20:11:23  来源:igfitidea点击:

How to remove class from all elements jquery

jquery

提问by maxum

I am changing the class of an element with the following

我正在使用以下内容更改元素的类

  $("#"+data.id).addClass("highlight")

Given the list below.

鉴于下面的列表。

 <div id="menuItems"> 
 <ul id="contentLeft" class="edgetoedge"> 
 <li  class="sep" ">Shakes and Floats</li> 
 <li id="297"><a href="#" onClick="cart('297','add')"><small>.00</small><b>Vanilla</b>     </a></li> 
 <li id="298"><a href="#" onClick="cart('298','add')"><small>.00</small><b>Peanut Butter</b></a></li> 
 <li id="299"><a href="#" onClick="cart('299','add')"><small>.00</small><b>Combo</b></a></li> 
 <li id="300"><a href="#" onClick="cart('300','add')"><small>.00</small><b>Chocolate</b></a></li> 
 <li id="301"><a href="#" onClick="cart('301','add')"><small>.00</small><b>Strawberry</b></a></li> 
 <li id="303"><a href="#" onClick="cart('303','add')"><small>.00</small><b>Banana</b></a></li> 
 <li id="304"><a href="#" onClick="cart('304','add')"><small>.00</small><b>Root Beer Float</b></a></li> 
 <li id="305"><a href="#" onClick="cart('305','add')"><small>.00</small><b>Espresso</b></a></li> 
 </ul>
 </div> 

I assumed I could remove the class with this...

我以为我可以用这个删除课程......

  $(".edgetoedge").removeClass("highlight");

But this doesn't work. How can I remove the class?

但这不起作用。如何删除课程?

回答by mellamokb

You need to select the litags contained within the .edgetoedgeclass. .edgetoedgeonly matches the one ultag:

您需要选择类中li包含的标签.edgetoedge.edgetoedge只匹配一个ul标签:

$(".edgetoedge li").removeClass("highlight");

回答by scrappedcola

try: $(".highlight").removeClass("highlight");. By selecting $(".edgetoedge")you are only running functions at that level.

试试:$(".highlight").removeClass("highlight");。通过选择,$(".edgetoedge")您仅在该级别运行功能。

回答by mu is too short

This just removes the highlightclass from everything that has the edgetoedgeclass:

这只是从具有highlight该类的所有内容中删除edgetoedge该类:

$(".edgetoedge").removeClass("highlight");

I think you want this:

我想你想要这个:

$(".edgetoedge .highlight").removeClass("highlight");

The .edgetoedge .highlightselector will choose everything that is a child of something with the edgetoedgeclass and has the highlightclass.

.edgetoedge .highlight选择会选择一切,是的东西与孩子edgetoedge阶级和有highlight阶级。

回答by Oliver Spryn

You could try this:

你可以试试这个:

 $(".edgetoedge").children().removeClass("highlight");

回答by cmplieger

$(".edgetoedge>li").removeClass("highlight");

回答by NAVNEET CHANDAN

The best to remove a class in jquery from all the elements is to target via element tag. e.g.,

从所有元素中删除 jquery 中的类的最佳方法是通过元素标记进行定位。例如,

$("div").removeClass("highlight");