应该只是这样:
// Gets the number of elements with class yourClassvar numItems = $('.yourclass').length附带说明一下,在链接对jQuery对象的许多函数之前,先检查length属性通常是有益的,以确保我们确实有一些工作要执行。见下文:
var $items = $('.myclass');// Ensure we have at least one element in $items before setting up animations// and other resource intensive tasks.if($items.length){ $items.animate() // It might also be appropriate to check that we have 2 or more // elements returned by the filter-call before animating this subset of // items. .filter(':odd') .animate() .end() .promise() .then(function () { $items.addClass('all-done'); });}


