“return this.each()”是什么意思?在 jQuery 中做什么?

发布于 2024-12-03 09:17:08 字数 360 浏览 0 评论 0原文

我正在寻找一个 jQuery 插件,它只有一个功能。通过构造函数参数设置适当的默认值后,该函数定义了几个辅助函数,然后作为最后一部分返回对 this.each()

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

的调用,如下所示:我了解 this.each() 在修改中的使用匹配 DOM 元素等,但是 return 语句完成什么作用呢?某种修改后的 DOM 元素数组,然后可以将其链接到其他调用中?

我在这个网站上读过有关 this.each 的内容,但我不太明白 return 在这里做什么。感谢您帮助解决这个问题。

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
});

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑾夏年华 2024-12-10 09:17:08

.each 返回它被调用的元素,因此在这种情况下,它可能是为了保持在该选择器上链接方法的能力。这意味着,如果插件的方法被称为 foo,您应该能够执行此操作,

$("mySelector").foo().show();

因为 foo 返回 .each 的结果,基本上是 < code>$("mySelector").

希望这是有道理的。

.each returns the elements it was called on, so in this case, it is probably to maintain the ability of methods to be chained on that selector. That means that if the plugin'S method is called foo, you should be able to do

$("mySelector").foo().show();

Because foo returned the result of .each which is basically $("mySelector").

Hope that made sense.

睫毛溺水了 2024-12-10 09:17:08

它允许人们在一堆元素上调用插件或事件,然后将相同的函数或事件应用于所有元素

所以如果你这样做:

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

如果,让我们说,.selector 包含 10 个元素,所有 10 个元素都会得到 myPlugin 所做的任何事情。


返回 .each 语句的原因是 .each() 返回给定的任何内容,并且它允许您在一个 jQuery 元素上将多个函数和插件链接在一起。

例如:

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

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

So if you do:

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

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


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.

For example:

$('.selector').myPlugin().yourPlugin();
半岛未凉 2024-12-10 09:17:08

这使得保持对象链成为可能,因此您可以像这样调用 jquery 方法:

$("selector").css().mouseover().etc().blahblah();

this make it possible to keep objects chain, so you can call jquery methods like this:

$("selector").css().mouseover().etc().blahblah();
云朵有点甜 2024-12-10 09:17:08

它返回 JQuery 对象,如 docs 中所述调用该方法。因此您可以对返回值调用另一个方法。

// without method chaining
myobject.a()
myobject.b()
myobject.c()

// with method chaining
myobject.a().b().c();

请参阅方法链流畅的界面

It returns the JQuery object which method is called on as stated in the docs. So you can call another method on the returned value.

// without method chaining
myobject.a()
myobject.b()
myobject.c()

// with method chaining
myobject.a().b().c();

See method chaining and fluent interface

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文