92 设置css 获取css 设置innerHTML 获取innerHTML

来源:互联网 发布:codol激光和握把数据 编辑:程序博客网 时间:2024/06/06 00:59
base.js:

var $=function()//调用
{
    return new Base();
};
//对象式
function Base()
{
    //创建一个数组来获取节点和节点的数组
    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)
{
    var element=this.elements[num];
    this.elements=[];//清空数组
    this.elements[0]=element;//重新赋值
    return this;
};
//设置css  获取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")//W3C
            {
                return window.getComputedStyle(this.elements[i],null)[attr];
            }
            else if(typeof this.elements[i].currentStyle !="undefined")//ie
            {
                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

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;
};
0 0
原创粉丝点击