当前位置:网站首页>Basic usage of set, map, DOM classlist in ES6
Basic usage of set, map, DOM classlist in ES6
2022-07-26 02:00:00 【Bald mule】
Catalog
1.set Define and initialize data
3.delete Deletes the specified element
4.has Query the specified element
2.delete Deletes the specified element
3.size obtain map The length of has Get the specified element
3、 ... and 、DOM classList Basic usage
One 、set Basic usage
It's like an array , But the values of the members are unique , There are no duplicate values . It's like an array , But the values of the members are unique , There are no duplicate values .
1.set Define and initialize data
const set = new Set([1,2,3,4,4]);
console.log(set);// Will automatically delete the duplicate 1,2,3,42.add add to
const set = new Set([1,2,3,4,4]);
set.add(5).add(6);
console.log(set);//1,2,3,4,5,63.delete Deletes the specified element
const set = new Set([1,2,3,4,4]);
set.add(5).add(6);
set.delete(6);
console.log(set);//1,2,3,4,5,4.has Query the specified element
const set = new Set([1,2,3,4,4]);
set.add(5).add(6);
set.delete(6);
console.log(set.has(6));//false5.clear Delete all
const set = new Set([1,2,3,4,4]);
set .clear();
console.log(set);//set(0)Two 、map data structure
1.set get Basic usage
const Info = {height:190,major:" Computer "};
const map = new Map();
// Set up map Value key->value
map.set("realname"," Zhang San ");
map.set("age",18);
map.set(Info," Personal details ");
//get obtain map Value Parameters :key
console.log(map.get("realname"));2.delete Deletes the specified element
// Delete map Value Parameters :key
map.delete("age");3.size obtain map The length of has Get the specified element
// obtain map The length of
console.log(map.size);
console.log("age Whether there is :" + map.has("age"));//falase Above to delete 4. obtain map All of key
const keys = map.keys();
console.log(keys);5.clear Delete
map.clear();3、 ... and 、DOM classList Basic usage
1. Complete code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.class1{
background: red;
}
.class2{
color:#fff;
}
.class3{
font-size: 20px;
}
</style>
</head>
<body>
<div id="demo"> I am a container </div>
<button id="btn1"> Control the color </button>
<button id="btn2"> Control font size </button>
<button id="btn3"> Determine whether there is a color style </button>
<button id="btn4"> Judging index 1 The name of the class </button><br/>
<button id="btn5"> Delete color style </button>
<button id="btn6"> Control font toggle The way </button>
<script>
let Demo = document.getElementById("demo");
let btn1 = document.getElementById("btn1");
let btn2 = document.getElementById("btn2");
let btn3 = document.getElementById("btn3");
btn1.addEventListener('click',()=>{
//classlist Add multiple styles
Demo.classList.add("class1","class2");
});
btn2.addEventListener('click',()=>{
Demo.classList.add("class3")
});
// classList.contains Determine whether there is a certain style
btn3.addEventListener('click',()=>{
console.log(Demo.classList.contains("class1")?" There is class1 The style of ":" non-existent class1 The style of ");
});
// classList.item Determine the class name of the specified subscript
btn4.addEventListener('click',()=>{
console.log(Demo.classList.item(1));
});
// classList.remove Delete style
btn5.addEventListener('click',()=>{
Demo.classList.remove("class1","class2");
});
btn6.addEventListener('click',()=>{
Demo.classList.toggle("class3");
// The second parameter Whether the style exists or not true Just force it false Force removal
Demo.classList.toggle("class3",false);
});
</script>
</body>
</html>2. Effect display

边栏推荐
- 一种MCU事件型驱动C框架
- 保护系统日志服务器和设备
- How uxdb works on multiple processors
- How to display numbers / English time in Excel
- Redis cluster construction (based on 6.x)
- # Dest0g3 520迎新赛(更新中)
- pdf. JS introduction
- P3166 number triangle (tolerance and exclusion +gcd)
- 【Verilog数字系统设计(夏宇闻)3-----Verilog语法的基本概念1】
- Composition API的优势
猜你喜欢

Zhinai buys melons (DP backpack)

Overview of database stress testing methods
![Web3.0 blog DAPP development practice [2022]](/img/18/f386246ff6ffbd0a42df57c3cd9170.png)
Web3.0 blog DAPP development practice [2022]

CD from grabbing the track to building a streaming media server -- a case study of "moon in the hometown of sleep"

# Dest0g3 520迎新赛(更新中)

Navica工具把远程MySQL导入到本地MySQL数据库

2022 love analysis ― bank digitalization practice report

PHP Alipay transfer to Alipay account

What is the difference between for... In... And for... Of

Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)
随机推荐
pt-onnx-ncnn转换的问题记录(接yolov5训练)
2022 love analysis ― bank digitalization practice report
JS add random pixel noise background to the page
Leetcode/ numbers that appear only once
Detailed explanation of redis6.x configuration parameters
Infinite fraction path (BFS pruning)
There is no setter method in grpc list under flutter. How to use related attributes
Excuse me, sir. Oracle to PG CDC Oracle, the upper case of the field is the same as that of PG
Leetcode/ numbers that appear only once
6 + 1 skills of Software Test Engineer
Typora expiration solution, what if typora can't open
Leetcode algorithm 147. insert and sort the linked list
The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
Dest0g3 520 orientation (under update)
SQL injection tutorial: learn through examples
How to use the pagoda panel to deploy the full stack project of node to the server
SQL manual blind injection and error reporting injection
Navica工具把远程MySQL导入到本地MySQL数据库
重发布基础与配置
E. OpenStreetMap (2D monotone queue)