Home > Article > Web Front-end > Detailed explanation of the steps of js encapsulation operation class
This time I will bring you a detailed explanation of the steps for jsencapsulation operationclass, what are the precautions for js encapsulation operation class, the following are Let’s take a look at practical cases.
1. Add class
function addClass(ele,cName) { var arr = ele.className.split(' ').concat(cName.split(" ")); for(var i=0;i<arr.length;i++){ for(var k=arr.length-1;k>i;k--){ (arr[k]==="")&&arr.splice(k,1); (arr[i]===arr[k])&&arr.splice(k,1); } } ele.className = arr.join(" "); }
2. Deleteclass
function removeClass(ele,cName) { var arr1 = ele.className.split(' '); var arr2 = cName.split(" "); for(var i=0;i<arr2.length;i++)for(var j=arr1.length-1;j>=0;j--)(arr2[i]===arr1[j])&&arr1.splice(j,1) ele.className = arr1.join(" ") }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related matters on the php Chinese website article!
Recommended reading:
Detailed explanation of the use of apply and call in JS (with code)
The above is the detailed content of Detailed explanation of the steps of js encapsulation operation class. For more information, please follow other related articles on the PHP Chinese website!