jQuery .each() this 和 element

标签 jquery

在 .each() 回调中,this 和回调函数的第二个参数之间有什么区别吗?

例如,在以下代码中:

$("example").each( function(index, element) {
    // body
});

thiselement 之间有什么区别吗?是否只是提供了第二个参数以便您可以选择名称?

最佳答案

不,没有区别;第二个参数只是为了方便。

Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

from http://api.jquery.com/each/

很可能,提供第二个参数是为了与 jQuery.each 保持一致。 .

关于jQuery .each() this 和 element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6377970/

相关文章:

javascript - 如何更改代码以从右向左移动?

jquery - exports 未在 Typescript 中定义

javascript - 尝试在 x 行上打印所有月份

javascript - jQuery @ 运算符?

jquery - 如何在jquery中获取只有月份和年份的月份差异

jQuery悬停()不适用于绝对定位的元素和动画

javascript - 掌握事件冒泡

javascript - 在 jQuery 中选择第 n 个 div(及以后)

jquery - 如何使用 jQuery 让某些东西消失?

javascript - 如何管理 RGraph 玫瑰图中 Canvas 的高度?