admin管理员组文章数量:1356733
I'm trying to learn jquery and I'm having some difficulty figuring out how to deal with a set of jquery results. Let's say I have some html like:
<div class="divClass">
<p class="pClass1">1</p>
<p class="pClass2">Some text.</p>
</div>
<div class="divClass">
<p class="pClass1">2</p>
<p class="pClass2">Some text.</p>
</div>
<div class="divClass">
<p class="pClass1">3</p>
<p class="pClass2">Some text.</p>
</div>
I want to loop through the div's with a class of "divClass", get the value contained in the child p with a class of "pClass1". Getting the set of div's with a class of "divClass" is easy with something like:
divs = $(".divClass");
But I'm not sure how to loop through "divs" and find the "pClass1" child, then get its value. Any help would be much appriciated.
I'm trying to learn jquery and I'm having some difficulty figuring out how to deal with a set of jquery results. Let's say I have some html like:
<div class="divClass">
<p class="pClass1">1</p>
<p class="pClass2">Some text.</p>
</div>
<div class="divClass">
<p class="pClass1">2</p>
<p class="pClass2">Some text.</p>
</div>
<div class="divClass">
<p class="pClass1">3</p>
<p class="pClass2">Some text.</p>
</div>
I want to loop through the div's with a class of "divClass", get the value contained in the child p with a class of "pClass1". Getting the set of div's with a class of "divClass" is easy with something like:
divs = $(".divClass");
But I'm not sure how to loop through "divs" and find the "pClass1" child, then get its value. Any help would be much appriciated.
Share Improve this question asked Nov 13, 2009 at 6:14 MichaelMichael 3273 silver badges9 bronze badges3 Answers
Reset to default 4You can use the each
method, and then, look for the '.pClass' elements, in the context of this
, which is the currently div being iterated:
var divs = $(".divClass");
divs.each(function () {
alert($('p.pClass1', this).text());
});
Check the above example here.
Use the each
method:
$(".divClass").each(function() {
});
There are lots of ways.
For your specific scenario, I would try something like this:
$(".divClass .pClass1").each(function() {
// Do whatever
});
If you wanted to do something with the div tags and the p tags, you could try the find method:
$(".divClass").each(function() {
var p1Tags = $(this).find(".pClass1");
});
本文标签: javascriptHow to loop though a jQuery result setStack Overflow
版权声明:本文标题:javascript - How to loop though a jQuery result set - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743987420a2571521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论