原生js仿jquery一些常用方法

來源:互聯網
上載者:User

標籤:title   .net   after   dom   等價   out   new   ldo   exp   

原生js仿jquery一些常用方法

下面小編就為大家帶來一篇原生js仿jquery一些常用方法(必看篇)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

 

最近迷上了原生js,能不用jquery等架構的情況都會手寫一些js方法,記得剛接觸前端的時候為了選取器而使用jquery。。。現在利用擴充原型的方法實現一些jquery函數:

1.顯示/隱藏

//hide() Object.prototype.hide = function(){  this.style.display="none";  return this; } //show() Object.prototype.show = function(){  this.style.display="block";  return this; } 

return this的好處在於鏈式調用。

2.滑動 省略speed和callback的傳入,因為要加一串判斷和處理回調,代碼量大

//slideDown() Object.prototype.slideDown = function(){  this.style.display = ‘block‘;  if(this.clientHeight<this.scrollHeight){   this.style.height=10+this.clientHeight+"px";   var _this = this;   setTimeout(function(){_this.slideDown()},10)  }else{   this.style.height=this.scrollHeight+"px";  } } //slideUp() Object.prototype.slideUp = function(){  if(this.clientHeight>0){   this.style.height=this.clientHeight-10+"px";   var _this = this;   setTimeout(function(){_this.slideUp()},10)  }else{   this.style.height=0;   this.style.display = ‘none‘;  } } 

3.捕獲/設定

//attr() Object.prototype.attr = function(){  if(arguments.length==1){   return eval("this."+arguments[0]);  }else if(arguments.length==2){   eval("this."+arguments[0]+"="+arguments[1]);   return this;  } } //val() Object.prototype.val = function(){  if(arguments.length==0){   return this.value;  }else if(arguments.length==1){   this.value = arguments[0];   return this;  } } //html() Object.prototype.html = function(){  if(arguments.length==0){   return this.innerHTML;  }else if(arguments.length==1){   this.innerHTML = arguments[0];   return this;  } } //text()需要在html()結果基礎上排除標籤,會很長,省略 

4.CSS方法

 

//css() Object.prototype.css = function(){  if(arguments.length==1){   return eval("this.style."+arguments[0]);  }else if(arguments.length==2){   eval("this.style."+arguments[0]+"=‘"+arguments[1]+"‘");   return this;  } } 

 5.添加元素

//append() Object.prototype.append = function(newElem){  this.innerHTML += newElem;  return this; } //prepend() Object.prototype.prepend = function(newElem){  this.innerHTML = arguments[0] + this.innerHTML;  return this; } //after() Object.prototype.after = function(newElem){  this.outerHTML += arguments[0];  return this; } //before() Object.prototype.before = function(newElem){  this.outerHTML = arguments[0] + this.outerHTML;  return this; } 

6.刪除/替換元素

//empty() Object.prototype.empty = function(){  this.innerHTML = "";  return this; } //replaceWith() Object.prototype.replaceWith = function(newElem){  this.outerHTML = arguments[0];  return this; } //remove() js內建,省略。 

7.設定css類

//hasClass() Object.prototype.hasClass = function(cName){  return !!this.className.match( new RegExp( "(\\s|^)" + cName + "(\\s|$)") ); } //addClass() Object.prototype.addClass = function(cName){  if( !this.hasClass( cName ) ){   this.className += " " + cName;  }  return this; } //removeClass() Object.prototype.removeClass = function(cName){  if( this.hasClass( cName ) ){   this.className = this.className.replace( new RegExp( "(\\s|^)" + cName + "(\\s|$)" )," " );  }  return this; } 

上面的設定CSS類也可以利用html5新API classList及contains實現 但不相容IE8以下及部分Firefox瀏覽器

Object.prototype.hasClass = function(cName){  return this.classList.contains(cName) } Object.prototype.addClass = function(cName){  if( !this.hasClass( cName ) ){   this.classList.add(cName);  }  return this; } Object.prototype.removeClass = function(cName){  if( this.hasClass( cName ) ){   this.classList.remove(cName);  }  return this; } 

9.選取器

//id或class選取器$("elem") function $(strExpr){  var idExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/;  var classExpr = /^(?:\s*(<[\w\W]+>)[^>]*|.([\w-]*))$/;  if(idExpr.test(strExpr)){   var idMatch = idExpr.exec(strExpr);   return document.getElementById(idMatch[2]);  }else if(classExpr.test(strExpr)){   var classMatch = classExpr.exec(strExpr);   var allElement = document.getElementsByTagName("*");   var ClassMatch = [];   for(var i=0,l=allElement.length; i<l; i++){    if(allElement[i].className.match( new RegExp( "(\\s|^)" + classMatch[2] + "(\\s|$)") )){     ClassMatch.push(allElement[i]);    }   }   return ClassMatch;  } } 

需要強調的是,選取器返回的結果或結果集包含的是htmlDOM,並非jquery的對象。大多數人都知道,document.getElementById("id")等價於jquery$("#id")[0],另外上面class選取器選擇的結果如需使用,需要利用forEach遍曆:

$(".cls").forEach(function(e){  e.css("background","#f6f6f6") }) 

10.遍曆 siblings()和children()擷取的結果也需要結合forEach使用

//siblings() Object.prototype.siblings = function(){  var chid=this.parentNode.children;  var eleMatch = [];  for(var i=0,l=chid.length;i<l;i++){   if(chid[i]!=this){    eleMatch.push(chid[i]);   }  }  return eleMatch; } //children() 原生js已含有該方法,故命名為userChildren。 Object.prototype.userChildren = function(){  var chid=this.childNodes;  var eleMatch = [];  for(var i=0,l=chid.length;i<l;i++){   eleMatch.push(chid[i]);  }  return eleMatch; } //parent() Object.prototype.parent = function(){  return this.parentNode; } //next() Object.prototype.next = function(){  return this.nextElementSibling; } //prev() Object.prototype.prev = function(){  return this.previousElementSibling; } 

jquery事件函數原生js已有,另外,原生js實現jquery的一個常用函數 ajax 將會在下一篇寫道。

 源:http://www.jb51.net/article/93036.htm

原生js仿jquery一些常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.