你的位置:首页 > 软件开发 > Java > prototype/constructor/__proto__之prototype简单应用

prototype/constructor/__proto__之prototype简单应用

发布时间:2016-02-01 13:00:05
一、简单使用构造原型加prototype造简单的轮子。1、想jQ那样获取HTML元素,先看JS代码 1 function Cmf() { //创建构造函数 2 3 this.arry = [] 4 5 } 6 Cmf.prototype.getNode = funct ...

prototype/constructor/__proto__之prototype简单应用

一、简单使用构造原型加prototype造简单的轮子。

1、想jQ那样获取HTML元素,先看JS代码

 1 function Cmf() { //创建构造函数 2  3   this.arry = [] 4    5 } 6 Cmf.prototype.getNode = function(tag, id) { //给构造函数定义一个getNode方法 7   if (tag.indexOf('.') == 0) { //如果传入的是类class形式 .nav 8     var oparent = id ? document.getElementById(id) : document; 9     var oparentallchild = oparent.getElementsByTagName("*"); //选取oparent里面的所有的子项目10     for (var i = 0; i < oparentallchild.length; i++) {11       12       if (oparentallchild[i].className == tag.substring(1)) {13         14         this.arry.push(oparentallchild[i])15       }16     }17   }else if(tag.indexOf('#') == 0&&id==null){ //如果传入的是id形式的 如#d18     this.arry.push(document.getElementById(tag.substring(1)))19   }else{ //如果传入的div 或者span形式的20     var Tagname=document.getElementsByTagName(tag)21     for(var i=0;i<Tagname.length;i++){22       this.arry.push(Tagname[i])23     }24   }25   return this26 }27 28 Cmf.prototype.html=function(h){ //设置获取html29   for(var i=0;i<this.arry.length;i++){30   if(h){31     this.arry[i].innerHTML=h32     return this33     }else{34       return this.arry[i].innerHTML35     }36   }37 38 }39 40 Cmf.prototype.css=function(attr,value){ //设置css样式41   console.log(this.arry)42   for(var i=0;i<this.arry.length;i++){43     if(value){44       console.log(this.arry[i])45       this.arry[i].style[attr]=value46       return this47     }else{48       if (typeof window.getComputedStyle != 'undefined') {//W3C49         return window.getComputedStyle(this.arry[i], null)[attr];50       } else if (typeof this.arry[i].currentStyle != 'undeinfed') {//IE51         return this.arry[i].currentStyle[attr];52       }53     }54   }55 }56 57 58 var cmf=function(){ //每次调用都实例化一次59   return new Cmf()60 }

原标题:prototype/constructor/__proto__之prototype简单应用

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录