Jquery 如何使用特定类计算DIV

Jquery 如何使用特定类计算DIV,jquery,Jquery,我想计算具有特定类名的div,然后在H2标记中添加数字 预期结果 <h2 id="result">5 Cards found</h2> <div class="post_card"></div> <div class="post_card"></div> <div class="post_card"></div> <

我想计算具有特定类名的div,然后在H2标记中添加数字

预期结果

<h2 id="result">5 Cards found</h2>

<div class="post_card"></div>
<div class="post_card"></div>
<div class="post_card"></div>
<div class="post_card"></div>
<div class="post_card"></div>
<代码>找到5张卡
您可以这样做:

$('#result .count').html($('.post_card').length)
我在html中添加了一个span yo作为计算值的容器,这样我们就不必在返回中包含找到的文本

演示

$('result.count').html($('post\u card').length)

找到的卡片

您可以这样做:

$('#result .count').html($('.post_card').length)
我在html中添加了一个span yo作为计算值的容器,这样我们就不必在返回中包含找到的文本

演示

$('result.count').html($('post\u card').length)

找到的卡片

以下是您的现场示例

$(文档).ready(函数(){
$('result').html($('post_card').length+“找到的卡片”);
});

发现五张卡

以下是您的现场示例

$(文档).ready(函数(){
$('result').html($('post_card').length+“找到的卡片”);
});

发现五张卡

您可以使用
长度
这样做

var count\u post=$(“.container.post\u card”).length;
var count_post_no=$(“.container.no_post_card”)。长度;
$(“#结果”).append(count#post);
$('result2')。追加(计数后编号)

结果(邮政卡):


结果(无邮政卡):
您可以这样做,使用
长度

var count\u post=$(“.container.post\u card”).length;
var count_post_no=$(“.container.no_post_card”)。长度;
$(“#结果”).append(count#post);
$('result2')。追加(计数后编号)

结果(邮政卡):


结果(无张贴卡):
这是任何jQuery教程的主题->->,我找到了这些,但不知道如何添加到h2或div中。所有这些结果都打印到控制台。这只是jQuery教程或上述学习中心的另一个要点,或者…-但事实并非如此(即使你肯定会找到关于该主题的问题),或者你将你所知道的添加到问题中,这样我们就不会在你已经知道的部分上浪费时间。这是任何jQuery教程的主题->->,我找到了这些,但不知道如何添加到h2或div中。所有这些结果都打印到控制台。这只是jQuery教程或上述学习中心的另一个要点,或者…-但事实并非如此(即使你肯定会找到关于这个主题的问题),或者你会在问题中添加你所知道的内容,这样我们就不会在你已经知道的部分上浪费时间。