将 vanilla JS forEach 重写为 jQuery Each()
Rewrite vanilla JS forEach into jQuery Each()
我在项目中使用 jQuery 时试图保持代码的一致性,但是,我对 jQuery 的 each()
方法有点困惑。
let cells=$(".cell").toArray();
我的 Vanilla JS 代码在这里:
cells.forEach(function(cell,index){
cell.addEventListener('click',function(){
console.log(cell,index);
})
})
我的 jQuery 在这里:
cells.each(function(index,cell){
cell.click(function(){
console.log(cell,index);
})
})
我知道代码是错误的,因为控制台显示 Uncaught TypeError: cells.each is not a function
但我检查了 jQuery 文档,它说 each
方法是 forEach
中的方法JS,我现在真的很迷茫
$(".cell").each
是一个函数。 $(".cell").ToArray().each
不是函数。
jQuery 的 each
方法存在于 jQuery 对象上,而不是普通 JS 数组。
我在项目中使用 jQuery 时试图保持代码的一致性,但是,我对 jQuery 的 each()
方法有点困惑。
let cells=$(".cell").toArray();
我的 Vanilla JS 代码在这里:
cells.forEach(function(cell,index){
cell.addEventListener('click',function(){
console.log(cell,index);
})
})
我的 jQuery 在这里:
cells.each(function(index,cell){
cell.click(function(){
console.log(cell,index);
})
})
我知道代码是错误的,因为控制台显示 Uncaught TypeError: cells.each is not a function
但我检查了 jQuery 文档,它说 each
方法是 forEach
中的方法JS,我现在真的很迷茫
$(".cell").each
是一个函数。 $(".cell").ToArray().each
不是函数。
jQuery 的 each
方法存在于 jQuery 对象上,而不是普通 JS 数组。