当前位置:网站首页>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

边栏推荐
- Digital transformation behind the reshaping growth of catering chain stores
- A MCU event driven C framework
- SVN version control branch and merge function use
- Republishing foundation and configuration
- The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
- Composition API的优势
- proto转换Dart | 项目使用Protobuf | flutter 使用grpc
- Why does the debugger display the wrong function
- How to use the pagoda panel to deploy the full stack project of node to the server
- pt-onnx-ncnn转换的问题记录(接yolov5训练)
猜你喜欢

Why does the debugger display the wrong function

【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】

BGP知识点总结

How to use the pagoda panel to deploy the full stack project of node to the server

DialogRPT-Dialog Ranking Pretrained Transformers

Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)

D. Rating compression (thinking + double pointer)

SQLyog数据导入导出图文教程

Characteristics and determination of neuraminidase from Clostridium perfringens in Worthington

P3166 number triangle (tolerance and exclusion +gcd)
随机推荐
AUTOCAD——计算面积的方法
Pt onnx ncnn conversion problem record (followed by yolov5 training)
C# 迭代器的实现
Summary after reading "poor dad and rich dad"
Dataframe modifies the value of a row or column position
pdf. JS introduction
[untitled]
[leetcode] 32. Longest valid bracket
Qt程序美化之样式表的使用方法,Qt使用图片作为背景与控件透明化,Qt自定义按钮样式
SQL manual blind injection and error reporting injection
Redis cluster construction (based on 6.x)
G2. passable paths (hard version) (tree diameter + LCA)
Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)
pt-onnx-ncnn转换的问题记录(接yolov5训练)
QT program beautification of the use of style sheets, QT uses pictures as the background and transparency of controls, QT custom button styles
When everything can be metauniverse, the development of metauniverse seems to have entered a new stage of development
【独立站建设】shopify卖家:学会这几点,网上商店销量翻倍!
登堂入室soc之编程基础环境变量设置
MySQL locking table problem
DialogRPT-Dialog Ranking Pretrained Transformers