Write a mini of jquery

Source: Internet
Author: User


(function () {

var _$ = window.$;
var _jquery = window.jquery;

var jQuery = Window.jquery = window.$ = function (selector) {

return new JQuery.fn.init (selector);
};


Jquery.fn = Jquery.prototype = {
Init:function (selector) {
var elements = Document.queryselectorall (selector);
Array.prototype.push.apply (this,elements);
return this;
},
JQuery: "1.0.0",
length:0,
Size:function () {
return this.length;
}

};
JQuery.fn.init.prototype = Jquery.fn;

Jquery.extend = JQuery.fn.extend = function () {
var o = arguments[0];
For (var p in O) {
THIS[P] = o[p];
}
};


Jquery.extend ({
Trim:function (text) {
Return (text| | ""). Replace (/^\s+|\s+$/g, "");
},
Noconflict:function () {
window.$ = _$;
Window.jquery = _jquery;
return jQuery;
}
});


JQuery.fn.extend ({
Get:function (num) {
return This[num];
},
Each:function (FN) {
for (var i = 0;i< this.length; i++) {
FN (I,this[i]);
}
return this;
},
Css:function () {
var L = arguments.length;
if (L = = 1) {
return this[0].style[arguments[0]];
} else {
var name = Arguments[0];
var value = arguments[1];
This.each (function (Index,ele) {
Ele.style[name] = value;

});
}
return this;
},
Hide:function () {//Hide element
This.each (function (Index,ele) {
Ele.style.display = "None";
});
},
Show:function () {//Display element
This.each (function (Index,ele) {
Ele.style.display = "block";
});
},
Addclass:function () {//Add (Class) class
var name = Arguments[0];
This.each (function (Index,ele) {
var ele_class = Ele.classname,
Blank = (Ele_class! = ")? ‘ ‘ : ‘‘;
Added = Ele_class + blank + name;
Ele.classname = added;
});
},
Removeclass:function () {//Delete (Class) class
var name = Arguments[0];
This.each (function (Index,ele) {
var obj_class = ' +ele.classname+ ';
Obj_class = Obj_class.replace (/(\s+)/gi, "),
removed = Obj_class.replace (' +name+ ', ');
removed = Removed.replace (/(^\s+) | ( \s+$)/g, ");
Ele.classname = removed;
});
},
Remove:function () {//delete property
var name = Arguments[0];
This.each (function (Index,ele) {
Ele.attributes.removeNamedItem (name);
});
},
Width:function () {//Set width
var name = Arguments[0];
This.each (function (Index,ele) {
Ele.style.width = name;
});
},
Height:function () {//Set height
var name = Arguments[0];
This.each (function (Index,ele) {
Ele.style.height = name;
});
},
Getwidth:function () {//Get object width
This.each (function (Index,ele) {
var GW = Ele.offsetwidth;
Console.log (GW);
});
},
Getheight:function () {//Get object height
This.each (function (Index,ele) {
var gh = ele.offsetheight;
Console.log (GH);
});
},
On:function (eventname,callback) {//on Event
This.each (function (Index,ele) {
Ele[eventname] = callback;
});
},
First:function () {//Gets the first child element of the element
This.each (function (Index,ele) {
var ss = Ele.children[0];
Console.log (ss);
});
},
Allele:function () {//Gets all child elements of the element
This.each (function (Index,ele) {
for (Var i=0;i<ele.children.length;i++) {
var ss = Ele.children[i];
Console.log (ss);
}

});
},

});

})();




Write a mini of jquery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.