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);