嗨我碰到这段代码,我特别想知道this.each(function(i,e)
和var $e = $(e);
是怎么回事。我想知道程序员在做什么。当你编写你自己的jquery函数时,这是什么意思?
谢谢!
$.fn.rssfeed = function (url, options, fn) {
return this.each(function (i, e) {
var $e = $(e);
var s = '';
}
嗨我碰到这段代码,我特别想知道this.each(function(i,e)
和var $e = $(e);
是怎么回事。我想知道程序员在做什么。当你编写你自己的jquery函数时,这是什么意思?
谢谢!
$.fn.rssfeed = function (url, options, fn) {
return this.each(function (i, e) {
var $e = $(e);
var s = '';
}
i
是.each
环路的当前迭代元素的索引。 e
是实际的DOM元素。
var $e = $(e);
分配$e
变量包装在jQuery对象,以便采取的jQuery的归一化的DOM方法利用当前DOM元素。
插件典型地获得适用于特定选择匹配,所以所有元素:
$("div").rssfeed(url, options, fn);
将导致插件遍历.each
环路中的所有div元素。
$.fn.rssfeed = function (url, options, fn) {
//Here this refers to the jquery object
//i refers to the index in the loop
//e refers to the dom element os $(e) will give the jquery object corresponding to the dom element
return this.each(function (i, e) {
var $e = $(e);
var s = '';
}
每个(函数(I,E))就像
for(var i = 0; i < this.length; i++){
var $e = $(this[i]);
}
事实上,E也许DOM元素和$(e)是只是用户的jQuery,使其成为一个对象($é );
附加信息:这是降低处理成本的好方法,而不是在需要时再次安装对象。 – Jules
对不起还是有点不清楚。现在是'$ e'引用一个DOM元素? – locoboy
不,$ e被分配了将'e'封装在jQuery对象中的结果。 'e'只是一个普通的DOM元素。 – karim79