写一个mini的JQuery
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写一个mini的JQuery相关的知识,希望对你有一定的参考价值。
(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(){//隐藏元素
this.each(function(index,ele){
ele.style.display = "none";
});
},
show:function(){//显示元素
this.each(function(index,ele){
ele.style.display = "block";
});
},
addClass:function(){ //添加(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(){ //删除(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(){ //删除属性
var name = arguments[0];
this.each(function(index,ele){
ele.attributes.removeNamedItem(name);
});
},
width:function(){ //设置宽度
var name = arguments[0];
this.each(function(index,ele){
ele.style.width = name;
});
},
height:function(){ //设置高度
var name = arguments[0];
this.each(function(index,ele){
ele.style.height = name;
});
},
getWidth:function(){ //获取对象宽度
this.each(function(index,ele){
var gw = ele.offsetWidth;
console.log(gw);
});
},
getHeight:function(){ //获取对象高度
this.each(function(index,ele){
var gh = ele.offsetHeight;
console.log(gh);
});
},
on:function(eventName,callback){//on事件
this.each(function(index,ele){
ele[eventName] = callback ;
});
},
first:function(){//获取该元素的第一个子元素
this.each(function(index,ele){
var ss = ele.children[0];
console.log(ss);
});
},
allEle:function(){//获取该元素的全部子元素
this.each(function(index,ele){
for(var i=0;i<ele.children.length;i++){
var ss = ele.children[i];
console.log(ss);
}
});
},
});
})();
以上是关于写一个mini的JQuery的主要内容,如果未能解决你的问题,请参考以下文章
jQuery - 01. jQuery特点如何使用jQueryjQuery入口函数jQuery和DOM对象的区别jQuery选择器
自己写bootloader——mini2440(初始化NAND FLASH)