[英]How do I count html elements whose class is the same using jquery or javascript, but inside a particular <ul>
I need to show the text inside "span" tags: "Show/hide replies" in my comments and subcomments system. 我需要在评论和子评论系统中的“ span”标签中显示文本:“显示/隐藏回复”。 So imagine a comment system to which you submit a comment and this comment shows on the page. 因此,请想象一个向您提交评论的评论系统,该评论将显示在页面上。 And then another person reply to this comment and this reply also shows immediately afterwards. 然后另一个人回复此评论,此回复也立即显示。
What I want is that this text "Show/hide replies" does not show on the page, when I don't have any replies, that is, some logic like: If(no reply){do not show "Show/hide replies"}. 我想要的是,当我没有任何答复时,此文本“显示/隐藏答复”不会显示在页面上,即某些逻辑,例如:If(no reply){不显示“显示/隐藏答复” “}。 Well, I created a jQuery code like this: 好吧,我创建了这样的jQuery代码:
$(document).ready(function(){
//This code below is to count how many classes ".div" I have
console.log($('.div').length);
if ($('.div').length > 0){
$('span.text').text("Show/hide replies")
}else{
}
});
And this is the html: 这是html:
<ul class="elements">
<span class="text"></span>
<li class="div">
<div class="wraparound">Isso aêeee</div>
</li>
<li class="div">
<div class="wraparound">Hein?</div>
</li>
</ul>
<ul class="elements">
<span class="text"></span>
<li class="div">
<div class="wraparound">O q faço?</div>
</li>
<li class="div">
<div class="wraparound">N vai me responder?</div>
</li>
</ul>
The classes must be named the same, because it's in reality part of a while loop (php), based on the database query (it queries how many replies there are in the database and shows these replies when its number is greater than zero, simple as that). 这些类必须命名相同,因为实际上它是while循环(php)的一部分,基于数据库查询(它查询数据库中有多少回复,并在其数量大于零时显示这些回复,简单)那样)。 The issue is that I want to count how many lists with class ".div" I have inside the "ul" tag (that means how many replies I have beneath the main comment). 问题是我想统计“ ul”标签中有“ .div”类列表的数量(这意味着我在主注释下有多少回复)。
If I activate the page inspector the browser console shows the total number of ".div" (4), but all I need is the number of ".div" inside the first ul and then inside the second one and so forth. 如果激活页面检查器,浏览器控制台将显示“ .div”的总数(4),但我需要的只是第一个ul内的“ .div”编号,然后是第二个ul内的编号,依此类推。 If the number of ".div" is zero, then the text "Show/hide replies" does not show. 如果“ .div”的数量为零,则不会显示“显示/隐藏回复”文本。 It only shows when the number of ".div" inside a particular "ul" (2) is greater than zero. 仅显示特定“ ul”(2)中“ .div”的数目大于零的时间。
But the console counts the total number (4). 但是控制台会计算总数(4)。 For that reason, if I have one reply (list with class ".div") in the first "ul", but none in the second "ul", even so, the console will count it, and as 1 is greater than zero, the text will appear above both the "ul", inside the tag "span". 因此,如果我在第一个“ ul”中有一个答复(列表为“ .div”类),但在第二个“ ul”中没有任何答复,则控制台将对其进行计数,因为1大于零,文字将显示在标签“ span”内的“ ul”上方。 How do I avoid this? 如何避免这种情况?
Well, I've just edited my question and I've put my span tags inside the "ul", so that it becomes a child. 好吧,我刚刚编辑了我的问题,并将span标签放在“ ul”中,这样它就变成了孩子。 According to @Amit Kumar's help, I rewrote my jQuery code and changed to this: 根据@Amit Kumar的帮助,我重新编写了jQuery代码并更改为:
$(document).ready(function(){
$('ul.elements').each(function(index,ulElement){
var listlength = $(ulElement).find('li.div').length;
console.log(listlength);
if (listlength > 0){
$('span.text').not($(this).next('span.text')).text("Show/hide replies"); //Yeah, but... Which span?
//$(this).next('span.text').text("");
}
});
});
The only problems are the spans. 唯一的问题是跨度。 I want the text to show only in the span(s) which is(are) child(ren) of the element "ul" whose number of "li.div" is greater than zero. 我希望文本仅显示在“ li.div”的数量大于零的元素“ ul”的子级中。 That means you can only show replies where there really are replies. 这意味着您只能在确实有回复的地方显示回复。
$("ul.elements").each((index,ulElement)=>{
const length = $(ulElement).find("li.div").length;
// length variable has number of replies for each post.
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.