js 获取id,class,元素
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>animate</title>
<styletype="text/css">
*{
margin:0px;
padding:0px;
border:0px;
}
#div1{
border:1px#000solid;
width:240px;
margin:0auto;
margin-top:120px;
height:240px;
}
</style>
</head>
<body>
<divid="div1">
div
</div>
<divclass="div">class1</div>
<span>span2</span>
<divclass="div">class2</div>
<span>span1</span>
<scripttype="text/javascript">
function$(element){
elem=newObject();
elem.element=element.split(":")[0];
elem.eq=element.split(":")[1]||-1;
elem.length=0;
elem.elem=[];
elem.get=function(){
vareh=elem.element.substr(0,1);
varel=elem.element.substr(1);
if(elem.element==""||null){
//元素为空;
emessage1="Elementisundefined!";
throw(emessage1)
}
else{
//获取元素
switch(eh){
case'#':this.elem.push(document.getElementById(el));break;
case'.':this.elem=this.byClass(el);break;
default:this.elem=this.byTag();break;
}
//判断获取的元素是否有
if(this.elem){
returnthis.elem;
}
else{
emessage2=elem.element+"isundefined!";
throw(emessage2);
}
}
};
//获取类
elem.byClass=function(el){
varaClass=[];
if(document.getElementsByClassName){
cls=document.getElementsByClassName(el);
for(vari=0;i<cls.length;i++){
aClass.push(cls[i]);
}
returnaClass;
}
else{
varalln=document.all||document.getElementsByTagName("*");
for(vari=0;i<alln.length;i++){
if(alln[i].className==el){
aClass.push(alln[i]);
}
};
returnaClass;
}
}
//获取tagName
elem.byTag=function(){
vardoc=[];
vardocn=document.getElementsByTagName(elem.element);
for(vari=0;i<docn.length;i++){
doc.push(docn[i]);
}
returndoc;
}
//设置样式
elem.css=function(porName,porStyle){
this.length=this.get().length;
for(vari=0;i<this.length;i++){
//this.style=(this.get()[i].getAttribute("style")==null)?"":this.get()[i].getAttribute("style");
this.style=(this.get()[i].style.cssText==null)?"":this.get()[i].style.cssText+";";
this.style+=porName+":"+porStyle+";";
//alert(this.style);
//this.get()[i].setAttribute("style",this.style)
this.get()[i].style.cssText=this.style;
}
};
returnelem;
}
window.onload=function(){
$(".div").css("background","black");
$("span").css("background","red");
$("span").css("color","blue");
//vardiv=document.getElementById("div1");
//div.style.cssText="background:red;"
}
</script>
</body>
</html>