admin管理员组

文章数量:1206765

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?

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

this.removeClass("playing");

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?

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

this.removeClass("playing");
Share Improve this question asked Apr 16, 2013 at 20:54 IljaIlja 46.5k103 gold badges289 silver badges525 bronze badges 1
  • Seriously, this is about as basic as you can get. Why don't you give just a tiny bit of effort, visit the jQuery docs, type in "parent" and see what comes up. Is that really too hard for you? – user1106925 Commented Apr 16, 2013 at 21:30
Add a comment  | 

5 Answers 5

Reset to default 10
this.parent().removeClass("playing");
$(this).closest('div').removeClass("playing")

or

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

JSfiddle Demo

<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>

本文标签: javascriptremove class from parent element with jQueryStack Overflow