我想知道为什么我得到错误“$ groceryItems [i] .detach不是一个函数”,当我能够看到$ groceryItems [i]确实是一个DOM元素的记录结果?为什么.detach()在DOM元素数组中抛出错误?
我只包括脚本而不是ejs视图。
<script>
function sortByAvgRating() {
var $groceryItems = $('.groceryItems');//grab all of the grocery items
for (var i = $groceryItems.length; i--;) {//detach each grocery item from the DOM
console.log('$groceryItems[i] ', $groceryItems[i]);
$groceryItems[i].detach();
}
$groceryItems.sort(function(a, b) {//sort the grocery items by the rating attribute
return +a.children[1].children[0].children[0].dataset.rating -
+b.children[1].children[0].children[0].dataset.rating;
}).
appendTo('groceryItemsContainer');//apppend the sorted array of elements to the DOM
}
</script>
因为......它不是一个函数一个DOM节点。 –