开发者

When you write your own jquery function, what does this mean?

开发者 https://www.devze.com 2023-03-24 22:36 出处:网络
Hi I came across this piece of code and I was wonderi开发者_运维问答ng specifically what is going on with this.each(function(i,e) and var $e = $(e);. I\'d like to know what the programmer is trying to

Hi I came across this piece of code and I was wonderi开发者_运维问答ng specifically what is going on with this.each(function(i,e) and var $e = $(e);. I'd like to know what the programmer is trying to do.

Thanks!

$.fn.rssfeed = function (url, options, fn) {
    return this.each(function (i, e) {
            var $e = $(e);
            var s = '';
}


i is the index of the currently iterated element of the .each loop. e is the actual DOM element.

var $e = $(e);

assigns the $e variable the current DOM element wrapped in a jQuery object in order to take advantage of jQuery's normalised DOM methods.

Plugins typically get applied to all elements matching a particular selector, so:

$("div").rssfeed(url, options, fn);

would lead to the plugin iterating over all div elements within the .each loop.


$.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 = '';
}


each(function(i, e)) just like

for(var i = 0; i < this.length; i++){
   var $e = $(this[i]);
}

Actually, e maybe a dom element and $(e) is just user jquery to make it be a object ($e);

0

精彩评论

暂无评论...
验证码 换一张
取 消