Base.js
var $=function()//調用
{
return new Base();
};
//對象式
function Base()
{
//創建1個數組來獲得節點和節點的數組
this.elements=[];//私有化,不共用
}
//獲得ID節點
Base.prototype.getId=function(id)
{
this.elements.push(document.getElementById(id));
return this;
};
//獲得元素節點
Base.prototype.getTagName=function(tag)
{
var tags=document.getElementsByTagName(tag);
for(var i=0;i<tags.length;i++)
{
this.elements.push(tags[i]);
}
return this;
};
//class獲得
Base.prototype.getClass=function(className)
{
var all=document.getElementsByTagName("*");
for(var i=0;i<all.length;i++)
{
if(all[i].className==className)
{
this.elements.push(all[i]);
}
}
return this;
};
//獲得某個節點
Base.prototype.eq=function(num)
//調用 $().getClass("className").eq(0).css("background","red")
{
var element=this.elements[num];
this.elements=[];//清空數組
this.elements[0]=element;//重新賦值
return this;
};
//設置css
Base.prototype.css=function(attr,value)
{
for(var i=0;i<this.elements.length;i++)
{
if(arguments.length==1)
{
if(typeof window.getComputedStyle!="undefined")
{
return window.getComputedStyle(this.elements[i],null)[attr];
}
else if(typeof this.elements[i].currentStyle !="undefined")
{
return this.elements[i].currentStyle[attr];
}
}
this.elements[i].style[attr]=value;
}
return this;
};
Base.prototype.click=function(fn)
{
for(var i=0;i<this.elements.length;i++)
{
this.elements[i].onclick=fn;
}
return this;
};
//設置innerHtml 獲得innerHTML
Base.prototype.html=function(value)
{
for(var i=0;i<this.elements.length;i++)
{
if(arguments.length==0)
{
return this.elements[i].innerHTML;
}
else
{
this.elements[i].innerHTML=value;
}
}
return this;
};
//添加class
Base.prototype.addClass=function(className)
{
for(var i=0;i<this.elements.length;i++)
{
if(!this.elements[i].className.match(new RegExp('(s|^)'+className+'(s|$)')))//判斷是不是已有這個class
{
this.elements[i].className+=' '+className;
}
}
return this;
};
//移出class
Base.prototype.removeClass=function(className)
//調用方法$().getClass("dd").addClass("a").addClass("b").removeClass("b");
{
for(var i=0;i<this.elements.length;i++)
{
if(this.elements[i].className.match(new RegExp('(s|^)'+className+'(s|$)')))//判斷是不是已有這個class
{
this.elements[i].className=this.elements[i].className.replace(new RegExp('(s|^)'+className+'(s|$)'),'');
}
}
return this;
};
//添加link或style的css規則,不經常使用
Base.prototype.addRule=function(num,selectorText,cssText,position)
//調用方法, $().addRule(0,"body","background:red",0)
{
var sheet=document.styleSheets[num];
if(typeof sheet.insertRule!="undefined")//w3c
{
sheet.insertRule(selectorText+"{"+cssText+"}",position);
}
else if(typeof sheet.addRule!="undefined")//iE
{
sheet.addRule(selectorText,cssText,position);//sheet.addRule("body","background:red",)
}
};
//移除link或style的css規則,不經常使用
Base.prototype.addRule=function(num,index) //調用方法, $().removeRule(0);
{
var sheet=document.styleSheets[num];
if(typeof sheet.deleteRule!="undefined")//w3c
{
sheet.deleteRule(index);
}
else if(typeof sheet.removeRule!="undefined")//iE
{
sheet.removeRule(index);//sheet.addRule("body","background:red",)
}
};
demo.js //調用base.js函數
window.onload=function()
{
// var base=new Base();
//alert(base.getId("box").elements)
$().getClass("dd").addClass("a").addClass("b").removeClass("b");
$().addRule(0,"body","background:red",0)
$().removeRule(0);
};