Add class, delete class, find class

Source: Internet
Author: User

Find class

function Getelementsbyclassname (parent,tagname,classname) {
var aele=parent.getelementsbytagname (tagName);
var arr=[];
for (var i=0; i<aele.length; i++)
{
var aclassname=aele[i].classname.split ('); //.................... (Subject to a space) at the time of splitting
for (var j=0; j<aclassname.length; J + +)
{
if (aclassname[j]==classname)
{
Arr.push (Aele[i]);
Break //............................. If you find one, jump out of the loop
}
}
}
return arr;
}

Determine if there are duplicates to use in conjunction with the Add class and delete class.

function Arrindexof (arr,v)
{
for (var i=0; i<arr.length; i++)
{
if (ARR[I]==V)
{
return i;
}
}
return-1;
}

Add Class function
function AddClass (obj,classname)
{
if (obj.classname== ')//........ .......... Determine if the object originally has no class to add the class directly
{
Obj.classname=classname;
}
else{
var arr2=obj.classname.split (');
var cun=arrindexof (arr2,classname);
if (Cun = =-1) {//... ...... ..... .......... The judging object didn't have this class and then added
Obj.classname + = ' +classname;
}
}
}

Delete a class function

function Removeclass (obj,classname)
{
if (obj.classname! = ")
{
var arr2=obj.classname.split (');
var cun=arrindexof (arr2,classname);
if (cun! =-1) {
Arr2.splice (cun,1); .... Splice (INDEX,HOWMANY,ITEM1), .......---------------------------()
Obj.classname=arr2.join (");
}
}

}

Knowledge Points:

Splice (INDEX,HOWMANY,ITEM1)

Index: Required. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.

Howmany: Required. The number of items to delete. If set to 0, the item is not deleted.

Item1: Optional. Adds a new item to the array.

Add class, delete class, find class

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.