为什么在 jQuery 插件中返回 this.each(function())? [英] Why return this.each(function()) in jQuery plugins?

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

问题描述

我看到的一些开发 jQuery 插件的教程和示例往往会返回

Some of the tutorials and examples I have seen for developing jQuery plugins tend to return

this.each(function () {
    //Plugin code here
});

在实例化插件的函数的末尾,但我还没有看到它背后的任何推理,它似乎是每个人都遵循的标准.谁能告诉我这种做法背后的原因?

at the end of the function that instantiates the plugin but I have yet to see any reasoning behind it, it just seems to be a standard that everyone follows. Can anyone enlighten me as to the reasoning behind this practice?

为了澄清,我的问题不是关于为什么要返回 this,而是为什么插件应该返回 this.each.

For clarification my question was not about why to return this, but rather why the plugin should return this.each.

推荐答案

当您使用选择器 ($('.myclass')) 过滤元素时,它可以匹配多个元素.
使用 each,您可以遍历 所有匹配的元素,并将您的代码应用于所有元素.

When you filter elements with a selector ($('.myclass')), it can match more than only one element.
With each, you iterate over all matched elements and your code is applied to all of them.

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

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