jQuery .each()此和元素 [英] jQuery .each() this and element

查看:76
本文介绍了jQuery .each()此和元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.each()回调内部,this与回调函数的第二个参数之间是否有区别?

Inside a .each() callback, is there any difference between this and the second argument of the callback function?

例如,在以下代码中:

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

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

is there any difference between this and element? Is the second argument just provided so you can choose a name?

推荐答案

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

Nope, there's no difference; the second argument is just for convenience.

每次运行回调时,都会从0开始传递当前循环迭代.更重要的是,回调是在当前DOM元素的上下文中触发的,因此关键字this引用了该元素.

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.

来自 http://api.jquery.com/each/

第二个参数很可能与 jQuery.each 保持一致.

Most likely, the second argument is provided for consistency with jQuery.each.

这篇关于jQuery .each()此和元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆