我正在尝试计算页面上的节标记,但每当此脚本运行时,它返回0.任何想法?
var sectionCount = $( "section").length;
console.log(sectionCount);
我指望的HTML如下:
<body>
<div class="container">
<section class="slide">1</section>
<section class="slide">2</section>
<section class="slide">3</section>
</div>
</body>
答案 0 :(得分:2)
只需添加一个$(document).ready()
子句,使其在加载html DOM后运行。
$( document ).ready(function() {
var sectionCount = $( "section").length;
console.log(sectionCount);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="container">
<section class="slide">1</section>
<section class="slide">2</section>
<section class="slide">3</section>
</div>
</body>
&#13;