如何找到具有特定类 JQuery 的 Div 数量

How to find the number of Divs with a certain Class JQuery?

本文关键字:JQuery Div 数量 何找      更新时间:2023-09-26

有没有办法找到浏览器中显示的 class=.class 的 DIV 数量?

像这样:

$('.class').Numberofobjects();

*显然,对象数量是虚构的。

谢谢!

泰勒

您需要 length 属性(并确保约束标记类型):

$('div.class').length

.length 将为您提供匹配的类列表

$('.class').length

原料药在这里

http://api.jquery.com/length/

<html>
<head>
    <script type="text/javascript" src="jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            alert($("div.a").length);
        });
    </script>
</head>
<body>
    <input type="button" id="mybutton" value="Click" />
    <div class="a">
    </div>
    <div class="a">
        <div class="a">
        </div>
    </div>
    <div class="a">
    </div>
</body>