93 adding a class to remove or remove a CSS rule from a link or style, not commonly used

Source: Internet
Author: User

Base.js



var $=function ()//Call
{
return new Base ();
};
//Object type
function Base ()
{
    //Create an array to get an array of nodes and nodes
this.elements=[];//privatization, not shared

}


//Get ID node
base.prototype.getid=function (ID)
{
This.elements.push (document.getElementById (ID));
return this;
};
//Get element node
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 Get
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;
};
//Get a node
base.prototype.eq=function (num) //Call $ (). GetClass ("ClassName"). EQ (0). CSS ("Background", "Red")
{
var element=this.elements[num];
this.elements=[];//emptying an array
this.elements[0]=element;//re-assigning values
return this;
};
//Set 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;
};
//Set innerHTML get 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;
};
//Add 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|$) '))//Determine if this class already exists
       {
this.elements[i].classname+= ' +classname;
       }
   }
return this;
};
//Move out of class
base.prototype.removeclass=function (ClassName) //Call Method $ (). 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|$) '))//Determine if this class already exists
        {
This.elements[i].classname=this.elements[i].classname.replace (New RegExp (' (\\s|^) ' +classname+ ' (\\s|$) '), ' );
        }
    }
return this;
};
//Add a CSS rule for link or style, not commonly used
base.prototype.addrule=function (num,selectortext,csstext,position)//Call method, $ (). 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",)
    }
};
//Remove the CSS rules for link or style, not commonly used
base.prototype.addrule=function (Num,index)//Call method, $ (). 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//Call base.js function

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);
};

93 adding a class to remove or remove a CSS rule from a link or style, not commonly used

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.