admin管理员组文章数量:1391925
I'm creating a directory of apps, each of which has it's own ments which are hidden until the user opens them. I'd like to show the number of ments that each app currently contains but I can only get it display the total number of ments on the page (See JSFiddle: / for a basic example). Ideally it should display 2 in the top box and 4 in the bottom box. I thought I might need to use an array or something along those lines?
Here is my code:
HTML
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
jQuery
$(".parent").each(function(index){
var numChilds = $(".child").length;
$(".parent").append(numChilds);
});
I'm creating a directory of apps, each of which has it's own ments which are hidden until the user opens them. I'd like to show the number of ments that each app currently contains but I can only get it display the total number of ments on the page (See JSFiddle: http://jsfiddle/9M4nw/2/ for a basic example). Ideally it should display 2 in the top box and 4 in the bottom box. I thought I might need to use an array or something along those lines?
Here is my code:
HTML
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
jQuery
$(".parent").each(function(index){
var numChilds = $(".child").length;
$(".parent").append(numChilds);
});
Share
Improve this question
edited May 30, 2013 at 11:52
SomeShinyObject
7,8316 gold badges40 silver badges60 bronze badges
asked May 30, 2013 at 11:50
AJTAJT
2662 gold badges8 silver badges33 bronze badges
4 Answers
Reset to default 4You have to select the current HTML element (with class parent
). Then, you will use it in selecting the length of elements with class .child
and the html for .counter
.
Using $(this)
you can select the current HTML element from each()
function.
Corrected code:
$(".parent").each(function(){
var numChilds = $(".child", $(this)).length;
$(".counter", $(this)).html(numChilds);
});
JSFIDDLE
The problem is you need to limit the scope of the element look up to the desired parent
element
$(".parent").each(function(index){
var numChilds = $(".child", this).length;
$(".counter", this).html(numChilds);
});
Another version
$(".parent").each(function(index){
var $this = $(this);
var numChilds = $this.find(".child").length;
$this.find(".counter").html(numChilds);
});
Demo: Fiddle
This would require some extra work to show the number of childs in a more elegan way, but it should be enough to understand the idea
$(".parent").each(function(index){
var $this = $(this);
var numChilds = $this.find(".child").length;
$this.append("<span>" + numChilds + "</span>");
});
This code will help you to get number of children separately for each parent div :-
$(document).ready(function () {
$(".parent").each(function (i) {
$(this).children().length;
});
});
Try this hope this may help you.Vote
本文标签: jQueryJavaScriptCounting child elements in multiple parent elementsStack Overflow
版权声明:本文标题:jQueryJavaScript - Counting child elements in multiple parent elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744731557a2622075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论