自己封裝jquery玩玩

'use strict';

functionZQuery(arg){

this.elements=[];

this.domString='';

switch(typeofarg){

case'string':

//元素? 獲取到

if(arg.indexOf('<')!=-1){

//存創建標簽

this.domString=arg;

}else{

this.elements=getEle(arg);

this.length=this.elements.length;

}

break;

case'function':

DOMReady(arg);

break;

default:

this.elements.push(arg);

break;

}

}

//把new ZQuery變成$

function$(arg){

return newZQuery(arg);

}

//DOMReady

functionDOMReady(fn){

if(document.addEventListener){

document.addEventListener('DOMContentLoaded',function(){

fn&&fn();

},false);

}else{

document.attachEvent('onreadystatechange',function(){

if(document.readyState=='complete'){

fn&&fn();

}

});

}

}

//css

ZQuery.prototype.css=function(name,value){

if(arguments.length==2){

for(vari=0; i

this.elements[i].style[name]=value;

}

}else{

//{} 'width'

if(typeofname=='string'){

returngetStyle(this.elements[0],name);

}else{

varjson=name;

for(vari=0; i

for(varnameinjson){

this.elements[i].style[name]=json[name];

}

}

}

}

};

//attr

ZQuery.prototype.attr=function(name,value){

if(arguments.length==2){

for(vari=0; i

this.elements[i].setAttribute(name,value);

}

}else{

//{} 'width'

if(typeofname=='string'){

return this.elements[0].getAttribute(name);

}else{

varjson=name;

for(vari=0; i

for(varnameinjson){

this.elements[i].setAttribute(name,json[name]);

}

}

}

}

};

//html

ZQuery.prototype.html=function(str){

if(str||str==''){

for(vari=0; i

this.elements[i].innerHTML=str;

}

}else{

return this.elements[0].innerHTML;

}

};

//val

ZQuery.prototype.val=function(str){

if(str||str==''){

for(vari=0; i

this.elements[i].value=str;

}

}else{

return this.elements[0].value;

}

};

//addClass

ZQuery.prototype.addClass=function(sClass){

for(vari=0; i

if(this.elements[i].className){

varre=newRegExp('\\b'+sClass+'\\b','g');

if(this.elements[i].className.search(re)==-1){

this.elements[i].className+=' '+sClass;

}

}else{

this.elements[i].className=sClass;

}

this.elements[i].className=this.elements[i].className.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');

}

};

//removeClass

ZQuery.prototype.removeClass=function(sClass){

for(vari=0; i

if(this.elements[i].className){

varre=newRegExp('\\b'+sClass+'\\b','g');

this.elements[i].className=this.elements[i].className.replace(re,'');

this.elements[i].className=this.elements[i].className.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');

if(this.elements[i].className==''){

this.elements[i].removeAttribute('class');

}

}

}

};

//show

ZQuery.prototype.show=function(){

for(vari=0; i

this.elements[i].style.display='block';

}

return this;

};

//hide

ZQuery.prototype.hide=function(){

for(vari=0; i

this.elements[i].style.display='none';

}

return this;

};

//animate

ZQuery.prototype.animate=function(json,options){

for(vari=0; i

move(this.elements[i],json,options);

}

};

//eq

ZQuery.prototype.eq=function(n){

return this.elements[n];

};

//index

ZQuery.prototype.index=function(){

varaEle=this.elements[0].parentNode.children;

for(vari=0; i

if(aEle[i]==this.elements[0]){

returni;

}

}

};

//jq轉原生

ZQuery.prototype.get=function(n){

return this.elements[n];

};

//事件

;'click mouseover mouseout mousedown mousemove mouseup mouseenter mouseleave keydown keyup contextmenu dblclick load error scroll resize'.replace(/\w+/g,function(str){

ZQuery.prototype[str]=function(fn){

for(vari=0; i

addEvent(this.elements[i],str,fn);

}

};

});

//hover

ZQuery.prototype.hover=function(fn1,fn2){

this.mouseenter(fn1);

this.mouseleave(fn2);

};

//toggle

ZQuery.prototype.toggle=function(){

vararg=arguments;

var_this=this;

for(vari=0; i

(function(count){

_this.elements[i].onclick=function(){

arg[count%arg.length]();

count++;

};

})(0);

}

};

//addEvent

functionaddEvent(obj,sEv,fn){

if(obj.addEventListener){

obj.addEventListener(sEv,function(ev){

varoEvent=ev||event;

if(fn(oEvent)==false){

oEvent.preventDefault();

oEvent.cancelBubble=true;

}

},false);

}else{

obj.attachEvent('on'+sEv,function(ev){

varoEvent=ev||event;

if(fn(oEvent)==false){

oEvent.cancelBubble=true;

return false;

}

});

}

}

//DOM插入

ZQuery.prototype.appendTo=function(str){

varaParent=getEle(str);

for(vari=0; i

//不為人知DOM操作

aParent[i].insertAdjacentHTML('beforeEnd',this.domString);

}

};

ZQuery.prototype.prependTo=function(str){

varaParent=getEle(str);

for(vari=0; i

//不為人知DOM操作

aParent[i].insertAdjacentHTML('afterBegin',this.domString);

}

};

ZQuery.prototype.insertAfter=function(str){

varaParent=getEle(str);

for(vari=0; i

//不為人知DOM操作

aParent[i].insertAdjacentHTML('afterEnd',this.domString);

}

};

ZQuery.prototype.insertBefore=function(str){

varaParent=getEle(str);

for(vari=0; i

//不為人知DOM操作

aParent[i].insertAdjacentHTML('beforeBegin',this.domString);

}

};

//remove

ZQuery.prototype.remove=function(){

for(vari=0; i

this.elements[i].parentNode.removeChild(this.elements[i]);

}

};

//插件

$.fn=ZQuery.prototype;

$.fn.extend=function(json){

for(varnameinjson){

ZQuery.prototype[name]=json[name];

}

};

$.ajax=function(json){

ajax(json);

};

$.jsonp=function(json){

jsonp(json);

};

functionjsonp(json){

varjson=json||{};

if(!json.url)return;

json.cbName=json.cbName||'cb';

json.data=json.data||{};

json.data[json.cbName]='show'+Math.random();

json.data[json.cbName]=json.data[json.cbName].replace('.','');

vararr=[];

for(varnameinjson.data){

arr.push(name+'='+json.data[name]);

}

//a=1&b=2

varstr=arr.join('&');

window[json.data[json.cbName]]=function(result){

json.success&&json.success(result);

oH.removeChild(oS);

};

varoH=document.head;

varoS=document.createElement('script');

oS.src=json.url+'?'+str;

oH.appendChild(oS);

}

//ajax

functionjson2url(json){

json.t=Math.random();

vararr=[];

for(varnameinjson){

arr.push(name+'='+json[name]);

}

returnarr.join('&');

}

functionajax(json){

varjson=json||{};

if(!json.url)return;

json.data=json.data||{};

json.type=json.type||'get';

if(window.XMLHttpRequest){

varoAjax=newXMLHttpRequest();

}else{

varoAjax=newActiveXObject('Microsoft.XMLHTTP');

}

switch(json.type.toLowerCase()){

case'get':

oAjax.open('GET',json.url+'?'+json2url(json.data),true);

oAjax.send();

break;

case'post':

oAjax.open('POST',json.url,true);

oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

oAjax.send(json2url(json.data));

break;

}

oAjax.onreadystatechange=function(){

if(oAjax.readyState==4){

if(oAjax.status==200){

json.success&&json.success(oAjax.responseText);

}else{

json.error&&json.error(oAjax.status);

}

}

};

}

//getStyle

functiongetStyle(obj,sName){

return(obj.currentStyle||getComputedStyle(obj,false))[sName];

}

//move

functionmove(obj,json,options){

varoptions=options||{};

options.duration=options.duration||700;

options.easing=options.easing||'ease-out';

varstart={};

vardis={};

for(varnameinjson){

start[name]=parseFloat(getStyle(obj,name));

dis[name]=json[name]-start[name];

}

varcount=Math.floor(options.duration/30);

varn=0;

clearInterval(obj.timer);

obj.timer=setInterval(function(){

n++;

for(varnameinjson){

switch(options.easing){

case'linear':

vara=n/count;

varcur=start[name]+dis[name]*a;

break;

case'ease-in':

vara=n/count;

varcur=start[name]+dis[name]*Math.pow(a,3);

break;

case'ease-out':

vara=1-n/count;

varcur=start[name]+dis[name]*(1-Math.pow(a,3));

break;

}

if(name=='opacity'){

obj.style.opacity=cur;

obj.style.filter='alpha(opacity:'+cur*100+')';

}else{

obj.style[name]=cur+'px';

}

}

if(n==count){

clearInterval(obj.timer);

}

},16);

}

//選擇器

functiongetByClass(obj,sClass){

if(obj.getElementsByClassName){

returnobj.getElementsByClassName(sClass);

}

varaResult=[];

varaEle=obj.getElementsByTagName('*');

varre=newRegExp('\\b'+sClass+'\\b','g');

for(vari=0; i

if(aEle[i].className.search(re)!=-1){

aResult.push(aEle[i]);

}

}

returnaResult;

}

functiongetByStr(aParent,str){

varaChild=[];

for(vari=0; i

switch(str.charAt(0)){

case'#':

varobj=document.getElementById(str.substring(1));

aChild.push(obj);

break;

case'.':

varaEle=getByClass(aParent[i],str.substring(1));

for(varj=0; j

aChild.push(aEle[j]);

}

break;

default:

if(/^\w+\.\w+$/.test(str)){

//li.on

vararr=str.split('.');

varaEle=aParent[i].getElementsByTagName(arr[0]);

varre=newRegExp('\\b'+arr[1]+'\\b','g');

for(varj=0; j

if(aEle[j].className.search(re)!=-1){

aChild.push(aEle[j]);

}

}

}else if(/^\w+\[\w+\=\w+\]$/.test(str)){

//input[type=text]

vararr=str.split(/\[|\=|\]/g);

varaEle=aParent[i].getElementsByTagName(arr[0]);

for(varj=0; j

if(aEle[j].getAttribute(arr[1])==arr[2]){

aChild.push(aEle[j]);

}

}

}else if(/^\w+\:\w+(\(\d+\))?$/.test(str)){

//li:first li:eq(0)

vararr=str.split(/\:|\(|\)/g);

varaEle=aParent[i].getElementsByTagName(arr[0]);

switch(arr[1]){

case'first':

aChild.push(aEle[0]);

break;

case'last':

aChild.push(aEle[aEle.length-1]);

break;

case'even':

for(varj=0; j

aChild.push(aEle[j]);

}

break;

case'odd':

for(varj=1; j

aChild.push(aEle[j]);

}

break;

case'eq':

aChild.push(aEle[arr[2]]);

break;

case'lt':

for(varj=0; j

aChild.push(aEle[j]);

}

break;

case'gt':

for(varj=arr[2]; j

aChild.push(aEle[j]);

}

break;

}

}else{

varaEle=aParent[i].getElementsByTagName(str);

for(varj=0; j

aChild.push(aEle[j]);

}

}

break;

}

}

returnaChild;

}

functiongetEle(str){

vararr=str.replace(/^\s+|\s+$/g,'').split(/\s+/g);

varaChild=[];

varaParent=[document];

for(vari=0; i

aChild=getByStr(aParent,arr[i]);

aParent=aChild;

}

returnaChild;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,908評論 18 139
  • 單例模式 適用場景:可能會在場景中使用到對象,但只有一個實例,加載時并不主動創建,需要時才創建 最常見的單例模式,...
    Obeing閱讀 2,097評論 1 10
  • 工廠模式類似于現實生活中的工廠可以產生大量相似的商品,去做同樣的事情,實現同樣的效果;這時候需要使用工廠模式。簡單...
    舟漁行舟閱讀 7,827評論 2 17
  • 當看到這句話的時候,我就在想,在這世上還能不能再找出一個如你一般的人,這樣的了解我、熟悉我、認識我、明白我? 答案...
    濁酒一壺慰風塵閱讀 806評論 0 1
  • 一次朋友喝醉了留宿在我家,我才知道,原來一場暗戀可以持續那么久,我也是第一次知道,有些暗戀會帶來那么深刻的傷害。 ...
    春上熊閱讀 519評論 4 1