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

Apr 16, 2018 am 09:40 AM
class javascript operate

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm usage tutorial: guide you in detail to run the operation

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

What is sudo and why is it important?

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

Linux Deploy operation steps and precautions

What to do if you forget to press F2 for win10 boot password What to do if you forget to press F2 for win10 boot password Feb 28, 2024 am 08:31 AM

What to do if you forget to press F2 for win10 boot password

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

Huawei Mate60 Pro screenshot operation steps sharing

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

Replace the class name of an element using jQuery

Learn to use common PHP built-in objects: master the operation and usage of built-in objects Learn to use common PHP built-in objects: master the operation and usage of built-in objects Jan 10, 2024 am 10:02 AM

Learn to use common PHP built-in objects: master the operation and usage of built-in objects

Discuz domain name modification operation guide Discuz domain name modification operation guide Mar 09, 2024 pm 04:36 PM

Discuz domain name modification operation guide

See all articles