jQuery: See how many elements a selector matched?
问题 If I have a selector like $.('.active'); How can I see how many items that matched? Alternatively, is there an easy way to see if more than zero elements were matched? 回答1: call .length on the returned set. Do not use .size because: The .size() method is deprecated as of jQuery 1.8 回答2: How many: var count = $('.active').length; Check if it matched something: if ($('.active').length) // since 0 == false 回答3: You can use the native javascript length property: alert( $(".active").length ); You