![](/img/trans.png)
[英]Remove a parent class from a child element before calling method on parent (javascript/jquery)
[英]remove class from parent element with jQuery?
我无法弄清楚如何从父元素中删除 class,基本上我有一个<audio>
标签(从现在起称为this
),它位于class="playing"
的 div 中 我如何删除这个 class?
试过这个,但比理解它会从音频元素而不是它的父 div 中删除 class:
this.removeClass("playing");
this.parent().removeClass("playing");
$(this).closest('div').removeClass("playing")
要么
$(this).closest('div.playing').removeClass('playing')
this.closest('div[class=playing]').removeClass("playing");
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");
$("input").keyup(function () { $(this).parent().removeClass('bg-red'); });
.bg-red { background-color: red; }.h-50 { height: 50px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="h-5 bg-red"> <h3>Add text inside input will remove "bg-red" class from parent div</h3> <input placeholder="Enter anything" type="text"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.