开发者

Count divs with a certain class

开发者 https://www.devze.com 2022-12-13 19:23 出处:网络
Seems pretty simple but I can\'t get it to work. I have two divs with the class \'user\'. I want to output \"you 开发者_开发百科have 2 divs\".

Seems pretty simple but I can't get it to work.

I have two divs with the class 'user'. I want to output "you 开发者_开发百科have 2 divs".

<script type="text/javascript">
    $(document).ready(function() {
        function divcount() {
            var mycount =  $('.user').length(); 
            document.write(mycount)
        }
    });
</script>

I'm sure I'm missing something simple..


It’s either $('.user').length (length property of Array) or $('.user').size() (size method of jQuery).


Length is a property not a function. Size is a function.


$(".user").length  // use the length property

$(".user").size()  // use the size method

notice that the code must be include in the $(function(){...}) block; like:

$(function(){
    alert( $(".user").length );
    alert( $(".user").size() );
});


It's just $('.user').length. It's a property, not a method call.

0

精彩评论

暂无评论...
验证码 换一张
取 消