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.
精彩评论