104 css選取器的的封裝 $("#ele").css() $(".ele").css() $("ele").css(),

來源:互聯網
上載者:User

104 css選取器的的封裝 $("#ele").css() $(".ele").css() $("ele").css(),

//demo.js  調用

window.onload = function () {
    // $().getClass("a").css("color","red");//舊方法
  /*  $("#box").css("color", "red").click(function(){
        alert($(this).html())
    });*/
    //$(".a").css("color","green")
   // $("p").find(".a").css("color","blue");
    $("div").find("span").css("color","red")
  // $("div").find(".a").css("color","red");
    //$("div").find("#bb").css("color","red")


//$(".a").css("color","red")
};


//Base.js


var $=function(args)//調用,把this傳遞過來
{
    return new Base(args);


};
//對象式
function Base(args)
{
    //建立一個數組來擷取節點和節點的數組
    this.elements=[];//私人化,不共用
    if(typeof args=="string")//$(".a").css("color","green")這樣調用執行這裡
    {
      switch (args.charAt(0))
      {
          case "#":
              this.elements.push(this.getId(args.substring(1)));
              break;
          case ".":
              this.elements=this.getClass(args.substring(1));//getClassreturn的是數組所以直接賦值
              break;
          default :
              this.elements = this.getTagName(args);//getClassreturn的是數組所以直接賦值
      }
    }
    else if(typeof  args=="object") // $().getClass("a").css("color","red");這樣調用就執行這裡
    {
        if(args!=undefined)//這裡的_this是一個對象,undefined也是個對象,卻別typeof放回的帶單引號的 “undefined”
        {
            this.elements[0]=args; //把this放到數組的第一個
        }
    }


}
//設定css選擇子節點
Base.prototype.find=function(str)
{
    var childElements=[];//臨時數組避免和base.element[]衝突
    for(var i=0;i<this.elements.length;i++)
    {
        switch (str.charAt(0))
        {
            case "#":
                childElements.push(this.getId(str.substring(1)));
                break;
            case ".":
              /*  var all=this.elements[i].getElementsByTagName("*");
                for(var j=0;j<all.length;j++)
                {
                    if(all[j].className==str.substring(1))
                    {
                        childElements.push(all[j]);
                    }
                }*/
                var temps = this.getClass(str.substring(1), this.elements[i]);
                for (var j = 0; j < temps.length; j ++) {
                    childElements.push(temps[j]);
                }
                break;
            default :
                var tags=this.elements[i].getElementsByTagName(str);
                for(var j=0;j<tags.length;j++)
                {
                    childElements.push(tags[j]);
                }
        }
    }
    this.elements=childElements;
    return this;
};


//擷取ID節點
Base.prototype.getId = function (id) {
    //返回ID節點的本身對象
    return document.getElementById(id);
};
//擷取元素節點數組
Base.prototype.getTagName = function (tag, parentNode) {
    var node = null;
    var temps = [];
    if (parentNode != undefined) {
        node = parentNode;
    } else {
        node = document;
    }
    var tags = node.getElementsByTagName(tag);
    for (var i = 0; i < tags.length; i ++) {
        temps.push(tags[i]);
    }
    return temps;
};
//class擷取
Base.prototype.getClass=function(className,parentNode)
{
    var node=null;
    var temps=[];//臨時數組避免和base.element[]衝突
    if(parentNode!=undefined)
    {
        node=parentNode;
    }
    else
    {
        node=document;
    }
    var all=node.getElementsByTagName("*");
    for(var i=0;i<all.length;i++)
    {
        if(all[i].className==className)
        {
            temps.push(all[i]);//給臨時數組添加
        }
    }
    return temps;  //返回臨時數組
};
//擷取某個節點,並且返回Base對象
Base.prototype.eq=function(num)
{
    var element=this.elements[num];
    this.elements=[];//清空數組
    this.elements[0]=element;//重新賦值
    return this;  //返回如jq的eq()一樣的東西
};


//擷取某個節點,並且放回這個節點的對象
Base.prototype.get=function(num)//
{
   return this.elements[num];//返回如jq的get()一樣的東西
};
//設定CSS
Base.prototype.css = function (attr, value) {
    for (var i = 0; i < this.elements.length; i ++) {
        if (arguments.length == 1) {
            return getStyle(this.elements[i], attr);//為什麼這裡需要return呢
        }
        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(!hasClass(this.elements[i],className))//判斷是否已經有這個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(hasClass(this.elements[i],className))//判斷是否已經有這個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",)
    }
};
//設定hover方法
Base.prototype.hover=function(over,out)
{
    for(var i=0;i<this.elements.length;i++)
    {
      //  this.elements[i].onmouseover=over;
       // this.elements[i].onmouseout=out;
        addEvent(this.elements[i],"mouseover",over);
        addEvent(this.elements[i],"mouseout",out)
    }
    return this;
};
//添加show
Base.prototype.show=function()
{
    for(var i=0;i<this.elements.length;i++)
    {
        this.elements[i].style.display="block"


    }
    return this;
};
//添加hide
Base.prototype.hide=function()
{
    for(var i=0;i<this.elements.length;i++)
    {
        this.elements[i].style.display="none"


    }
    return this;
};
//設定物體置中
Base.prototype.center=function(width,height)
{
    var top=(getInner().height-width)/2;
    var left=(getInner().width-height)/2;
    for(var i=0;i<this.elements.length;i++)
    {
        this.elements[i].style.top=top+"px";
        this.elements[i].style.left=left+"px";
    }
    return this;
};
//觸發瀏覽器視窗事件
Base.prototype.resize = function (fn) {
    for(var i=0;i<this.elements.length;i++)
    {
        var element=this.elements[i];
       /* window.onresize=function()
        {
            fn();
            if (element.offsetLeft > getInner().width - element.offsetWidth) {
                element.style.left = getInner().width - element.offsetWidth + 'px';
            }
            if (element.offsetTop > getInner().height - element.offsetHeight) {
                element.style.top = getInner().height - element.offsetHeight + 'px';
            }
        }*/
        addEvent(window,"resize",function()
        {
            fn();
            if (element.offsetLeft > getInner().width - element.offsetWidth) {
                element.style.left = getInner().width - element.offsetWidth + 'px';
            }
            if (element.offsetTop > getInner().height - element.offsetHeight) {
                element.style.top = getInner().height - element.offsetHeight + 'px';
            }
        })
    }
    return this;
};
//鎖屏功能
Base.prototype.lock=function()
{
    for(var i=0;i<this.elements.length;i++)
    {
        this.elements[i].style.width=getInner().width+"px";
        this.elements[i].style.height=getInner().height+"px";
        this.elements[i].style.display="block";
        document.documentElement.style.overflow = 'hidden';
        addEvent(window,"scroll",scrollTop);
    }


    return this;
};
function scrollTop(){
    document.documentElement.scrollTop=0;// IE W3C
    document.body.scrollTop=0;//Firefox
}
Base.prototype.unlock=function()
{
    for(var i=0;i<this.elements.length;i++)
    {
        this.elements[i].style.display="none";
        document.documentElement.style.overflow = 'auto';
        removeEvent(window,"scroll",scrollTop);
    }


    return this;
};
//外掛程式入口
Base.prototype.extend=function(name,fn)
{
    Base.prototype[name]=fn;
};


$().extend("bbb",function(){
   alert("123")
});










聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.