jQuery: Get tag inside element with class
<div id="yes">
<div class="wr开发者_开发技巧apper">
<something>...</else>
<p>foobar</p>
</div>
</div>
I want to get the p-tag to append a classname
$("div.wrapper", "#yes").addClass("newClassName");
But where to add the "p"?
Try
$("div#yes > div.wrapper > p").addClass("newClassName");
Here is a jsFiddle demo
This can be achieved by the folowing code:
$("#yes div.wrapper p").addClass("newClassName");
This code gets any p-tag within any div with the class "wrapper" which is located in an element with the id "yes".
$(".wrapper").find("p").addClass("newClassName");
$("#yes").find("p").addClass("newClassName");
精彩评论