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

查看:107
本文介绍了为什么要在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.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天全站免登陆