“返回this.each()"的含义是什么?在jQuery中做? [英] what does "return this.each()" do in jQuery?

查看:119
本文介绍了“返回this.each()"的含义是什么?在jQuery中做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看一个具有单个功能的jQuery插件.通过构造函数参数设置适当的默认值后,该函数定义了几个辅助函数,然后在最后一部分返回对this.each()的调用,如下所示:

I'm looking at a jQuery plugin, which has a single function. After setting up the appropriate defaults though a constructor argument the function defines a couple of helper functions, and then as the last part returns a call to this.each(), like so:

return this.each(function() {
 //long method defined here
});

我了解this.each()在修改匹配的DOM元素等中的用途,但是return语句完成什么工作?某种类型的修改后的DOM元素数组,然后可以在其他调用中链接它们?

I understand the use of this.each() in modifying matching DOM elements and such, but what does the return statement accomplish? Some sort of array of modified DOM elements, which can then be chained in other calls?

我已经在网站上阅读了有关此内容的每篇文章,但是我不太清楚return在这里的作用.感谢您帮助解决此问题.

I've read about this.each on this site but I can't quite figure what the return does here. Thanks for helping clear this up.

推荐答案

它允许调用一堆元素上的插件或事件,然后将相同的函数或事件应用于 all 其中

It allows for one to call a plugin or an event on a bunch of elements and then apply that same function or event to all of them

因此,如果您这样做:

$('.selector').myPlugin();

如果让我们说.selector包含10个元素,那么所有10个元素都会得到myPlugin的作用.

And if, let us say, .selector contains 10 elements, all 10 elements would get whatever myPlugin does.

之所以返回该.each语句,是因为.each()返回所给出的内容,并且它允许您将多个函数和插件链接到一个jQuery元素上.

The reason for returning that .each statement is because .each() returns whatever it was given and it allows you to chain multiple functions and plugins together on one jQuery element.

例如:

$('.selector').myPlugin().yourPlugin();

这篇关于“返回this.each()"的含义是什么?在jQuery中做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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