$('elems').each() 带粗箭头 [英] $('elems').each() with fat arrow

查看:35
本文介绍了$('elems').each() 带粗箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 ES6 粗箭头函数表示法,我非常喜欢它.但我对它的上下文有点困惑.据我所知,胖箭头函数中的 this 关键字是指函数当前运行的上下文.我想做一些简单的 jQuery 迭代,例如:

I started to use ES6 fat arrow function notation and I really like it. But I am a little bit confused about it context. As far as I know, keyword this inside fat arrow function refers to context where the function is currently running. I wanted to do some simple jQuery iteration like:

$('ul#mylist > li').each(() => {
   $(this).addClass('some-class-name');
});

但显然这段代码不起作用.我如何在胖箭头函数中引用此特定代码中的当前LI"元素?

But obviously this piece of code not working. How do I refer, inside fat arrow function, to current "LI" element in this specific code?

推荐答案

each() 方法为回调函数提供两个参数.它们是当前索引和当前项目.因此,您可以执行以下操作:

The each() method supplies two parameters to the callback-function. They are current index and the current item. Thus you could do the following:

$('ul#mylist > li').each((i, v) => {
   $(v).addClass('some-class-name');
});

其中v"变量是当前的li"元素

Where the "v" variable is the current "li" element

这篇关于$('elems').each() 带粗箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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