Home > Web Front-end > JS Tutorial > Detailed explanation of the steps of js encapsulation operation class

Detailed explanation of the steps of js encapsulation operation class

php中世界最好的语言
Release: 2018-04-16 09:40:53
Original
2167 people have browsed it

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(" ");
}
Copy after login

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(" ")
}
Copy after login

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:

JS realizes digital changes

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template