计算一篇文章中有多少节

Count how many sections in a article

本文关键字:多少 文章 一篇 计算      更新时间:2023-09-26

至少我想计算一篇文章中有多少节。如果这不适用于此元素,我也可以使用普通的"div"标签代替。对不起,我的英语不好。

<article>
    <section>
        <p>text</p>
    </section>
    <section>
        <p>example</p>
    </section>
</article>

您可以使用:

$("article section").length

试试这个:

     <div>
         <p></p>
         <p></p>
         <p></p>
         <p></p>
         <p></p>
     </div>

脚本:

     var a=$("div p").length;
     alert(a);

如果您有多个<article>,并且想知道每个中有多少个:

$('article').each(function(){
   var secCount = $(this).find('section').length;
});