您的位置:首页 > Web前端 > JQuery

jQuery源码分析-----仿栈与队列操作之get | eq

2017-07-07 10:57 477 查看

get与eq都是获取指定索引的元素

get返回的是DOM对象,直接将querySelectorAll返回的第
i
个元素赋值给
this[i],get[i]
this[i]
即可

eq
返回的是
jQuery
对象
,即在用
pushStack将this[i]
处理。

var $$ = ajQuery = function (selector) {
return new ajQuery.fn.init(selector);
};
ajQuery.fn = ajQuery.prototype = {
init:function (selector) {
this.selector = selector;
var results = document.querySelectorAll(selector);
for(var i = 0;i<results.length;i++){
this[i] = results[i];
}
return this;
},
constructor:ajQuery
};
ajQuery.prototype.init.prototype = ajQuery.prototype;
ajQuery.extend = ajQuery.fn.extend = function () {
var target = arguments[0];
if(arguments.length == 1){
target = this;
}
for(var i = 0;i<arguments.length;i++){
if((options = arguments[i]) != null) {
for(var option in options){
target[option] = options[option];
}
}
}
return target;
};
ajQuery.fn.extend({
get:function (num) {{
if(num != null){
return (num<0? this[this.length+num] :this[num]);
}else{
return [].slice.call(this);//返回所有元素
}
}},
setName:function (name) {
this.name = name;
return this;
},
getName:function () {
console.log(this.name);
return this;
}
});
var li = $$('li').setName('miemie').getName().get(2);
console.log(li);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery