函数$的扩展,返回带有常用方法的HTML元素对象

 

 

//**************
// for IE 6.0+
// v 0.6
// dommy529
//**************

function $(o){
var e = null;
if(o == ""){
   e = document.body;
}else{
   switch(typeof o){
    case 'undefined' :
     e = document.body;
     break;
    case 'object' :
     e = o;
     break;
    case 'string' :
     e = document.getElementById(o);
     break;
    case 'array' :
     for(var i=0;i<o.length;i++){
      e = $(o[i]);
     }
     break;
    default :
     e = null;
     break;
   }
}
if(e!=null && e!=document.body && e.tagName && !e.setClassName){
   var t = e.tagName.toUpperCase();
   if("DIV/SPAN/A/P/LI/UL/FORM".indexOf(t)>=0){
    LBL.call(e);
   }else if(t=="INPUT"){
    INP.call(e);
   }else if(t=="IMG"){
    IMG.call(e);
   }
}
return e;
}

// HTML元素扩展类 封装常用方法
function ELM(){ // 基类
this.setClassName = function(cls){this.className = cls;}
this.setStyle = function(style){
   if(typeof style != "object"){return false;}
   for(var i in style){
    this.style[i] = style[i];
   }
}
this.show = function(){this.setStyle({'display':''});}
this.hide = function(){this.setStyle({'display':'none'});}
this.hideorshow = function(){
   if(this.style.display == 'none'){
    this.show();
   }else{
    this.hide();
   }
}
this.moveTo = function(l,t){
   if(this.style.position!='absolute' || typeof l == "undefined" || typeof t == "undefined"){return false;}
   var arg = {};
   if(l==""){
    if(t!=""){arg['top']=t;}
   }else{
    if(t==""){
     arg['left'] = l;
    }else{
     arg['top'] = t;
     arg['left'] = l;
    }
   }
   this.setStyle(arg);
}
this.getPOS = function(){
   var p = {};
   p['left'] = this.offsetLeft;
   p['top'] = this.offsetTop;
   var f = this.offsetParent;
   while(f){
    p['left'] += f.offsetLeft;
    p['top'] += f.offsetTop;
    f = f.offsetParent;
   }
   return p;
}
this.setEvent = function(evt,fun){this.attachEvent("on"+evt,fun);}
this.delEvent = function(evt,fun){this.detachEvent("on"+evt,fun);}
this.popup = function(o,p){
   if(this.tagName.toUpperCase()!='IMG'){
    if(typeof o == 'object'){
     this.onmouseover = function(){this.setStyle(p)};
     this.onmouseout = function(){this.setStyle(o)};
    }else if(typeof o == 'string'){
     this.onmouseover = function(){this.setClassName(p)};
     this.onmouseout = function(){this.setClassName(o)};
    }
   }else{
    this.onmouseover = function(){this.setSrc(p)};
    this.onmouseout = function(){this.setSrc(o)};
   }
}
this.unpop = function(){
   this.onmouseover = function(){return false};
   this.onmouseout = function(){return false};
}
}
//区块类型元素
function LBL(){
ELM.call(this);
this.setInnr = function(str,tp){
   tp = typeof tp == "undefined" ? 0 : tp;
   switch(tp){
    case 0 : this.innerHTML = str; break;
    case 1 : this.innerHTML += str; break;
    case 2 : this.innerHTML = str+this.innerHTML; break;
   }
}
this.getChilds = function(){
   var c = this.childNodes;
   var o = [];
   for(var i=0;i<c.length;i++){
    o[i] = $(c[i]);
   }
   return o;
}
}
//input元素
function INP(){
ELM.call(this);
this.setValue = function(str,tp){
   tp = typeof tp == "undefined" ? 0 : tp;
   switch(tp){
    case 0 : this.value = str; break;
    case 1 : this.value += str; break;
    case 2 : this.value= str+this.value; break;
   }
}
this.enable = function(){this.disabled = false;}
this.disable = function(){this.disabled = true;}
this.lock = function(){this.readonly = true;}
}
//image元素
function IMG(){
ELM.call(this);
this.setSrc = function(src){this.src = src;}
}

 

 

封装一下以后可以简单的操作DOM,例如

改style    $('id').setStyle({'width':'250px','height':'100px'}); $('id').hide();

设置鼠标翻转 $('id').popup({'color':'#000'},{'color':'#FF0000'});

绑定动作等 $('id').setEvent('click',Function);

 

附一个简单例程:

<html>
<head>
<script type="text/javascript" src="$.js"></script>
</head>
<body>
<div id="kk">
<span>11</span>
<span>11</span>
<span>11</span>
<span>11</span>
</div>
<input id="btn" type="button" value="dianwo">
<script>
function doit(){
var o = $('kk');
o.setStyle({'backgroundColor':'#FF0000',
     'width':'350px',
     'height':'100px',
     'color':'#FFF'});
o.setInnr('Hello World!',1);
var oc = o.childs();
oc[0].setStyle({'color':'#000'});
}
</script>
<img id="img" src="images/nav_bg.png">
<script>
var img = $('img');
img.setStyle({'border':'#0000FF solid 1px'});
img.popup('images/nav_bg.png','images/nav_bg_p.png');
var btn = $('btn');
btn.popup({'color':'#000'},{'color':'#ff0000'});
btn.setEvent("click",doit);
</script>
</body>
</html>

 


 
  网址或邮箱: (选填) <script></script>   内 容:
插入表情
 闪光字
<script></script> <script></script>   验证码: 请点击后输入四位验证码,字母不区分大小写
<script type="text/javascript"></script>       

   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值