javascript 使用 jQuery 从父元素中删除类?

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

remove class from parent element with jQuery?

javascriptjquery

提问by Ilja

I can't figure out how to remove class from a parent element, basically I have a <audio>tag (from now on referred to as this) which is inside a div with class="playing"how can I remove this class?

我不知道如何从父元素中删除类,基本上我有一个<audio>标签(从现在开始称为this),它位于 div 内,class="playing"如何删除此类?

tried this, but than understood that it will remove class from audio element not it's parent div:

试过这个,但比理解它会从音频元素中删除类而不是它的父div:

this.removeClass("playing");

回答by Uncle Iroh

this.parent().removeClass("playing");

回答by Schleis

$(this).closest('div').removeClass("playing")

or

或者

$(this).closest('div.playing').removeClass('playing')

回答by Mohammad Adil

this.closest('div[class=playing]').removeClass("playing");

回答by dekdev

JSfiddle Demo

JSfiddle演示

<div class="bold">
<p id="p1" class="blue under">Hello</p>
</div>
<div class="bold">
  <p  id="p2" class="blue under highlight">and</p>
</div>
  <p class="blue under">then</p>
  <p class="blue under">Goodbye</p>


$("#p1").parent().removeClass("bold");