当前位置:网站首页>Native JS dynamically add and delete classes
Native JS dynamically add and delete classes
2022-06-22 10:36:00 【Ah Fei】
Native JS Dynamically add and delete classes
Because of the need to , Listen for click events for button groups ( Request to delegate with an event ), When a button is clicked , Add a class to the button accordingly ( Activate class ), Other buttons that have not been clicked will be removed from this category
There are three ways to add and remove classes
First get a dom object ( Also called dom Elements ), adopt document.getElement…… By several methods
Such as
<div id="box" class="foo"></div>
==>
<div id="box" class="foo bar"></div>
let el = document.getElementById("box");
By class name , Get the name of the class :
el.className, assignment :el.className = "foo bars"Will overwrite the original classPassing attribute , Get the name of the class :
el.getAttribute("class");assignment :el.setAttribute("class", "foo bar");Will overwrite the original classThrough the attribute node
attributeNode( Poor performance , But it's compatible ie,getAttribute()ie Some versions of do not support )setAttributeNode()Method to add the specified attribute node to the element .
If the specified property already exists , Then this method will replace it ., Get the name of the class :getAttributeNode("class").value, assignment :let attr = document.createAttribute("class"); attr.nodeValue = "foo bar"; el.setAttributeNode(attr)adopt classList attribute , Get the name of the class
el.classList;Append class name :el.classList.add("foo");Delete class :el.calssList.remove("foo");
The above four methods , classList Most flexible , Better be easy to use , But not supported ie9 The following browsers , Poor compatibility
Sample code :
html
<div id="btn-group">
<div class="btn btn-active"> Button 1</div>
<div class="btn"> Button 2</div>
<div class="btn"> Button 3</div>
<div class="btn"> Button 4</div>
</div>
js Code , Which USES ES6 grammar ( use ES6 Write concisely )
let myEventUtil = {
// Add listening Events
addEvent (element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attach){
// ie
element.attach("on"+ type, handler);
} else {
element['on' + type] = handler;
}
},
getTarget (event) {
let event = event || window.event;
return event.target || event.srcElement;
}
}
let my$ = id => document.getElementById(id);
let btnGroup = my$(“btn-group”);
myEventUtil.addEvent(btnGroup, 'on', function (ev) {
// To all btn Remove the active class btn-active
// console.log(this) ==> It's a dom Elements btnGroup
// Can pass el.children[i] Get the specific child elements
// You can get the child elements by el.classList.remove("className") Delete class
// el.classList.add("className") To add classes
// Delete class
let len = this.children.length;
for (let i = 0; i < len; i ++) {
this.children[i].classList.remove("btn-active");
// this.children[i].className = "btn"; // Just use one of them
}
// Add the class , Get specific btn Add classes to it
myEventUtil.getTarget(ev).classList.add("btn-active");
// myEventUtil.getTarget(ev).className = "btn"; // Just use one of them
});
边栏推荐
- M2 芯片解析:似乎是一个增强版的 A15?
- 中坚力量!优炫软件入选2022年中国数字安全百强
- [popular science] to understand supervised learning, unsupervised learning and reinforcement learning
- 每日一题day5-1636. 按照频率将数组升序排序
- [deep learning] tensorflow, danger! Google itself is the one who abandoned it
- TCP异常连接
- 传iPhone 14将全系涨价;TikTok美国用户数据转移到甲骨文,字节无法访问;SeaTunnel 2.1.2发布|极客头条...
- 被曝泄露超 1.7 亿条隐私数据,学习通回应:尚未发现明确证据
- Detailed explanation of rules and ideas for advance sale of deposit
- The ruby code in logstash converts the @timestamp timestamp format
猜你喜欢
随机推荐
【这款工具配合jmeter,会让你的工作效率至少提升80%,强烈推荐给大家】
Tiktok announces data storage on Oracle server!
Pycharm调试卡住,出现connected
牛客网——华为题库(31~40)
[untitled] repair log
TCP异常连接
Byte 2: why is the key of the redis master node expired, but the expired data is still read from the secondary node? How to solve it?
Analysis of thinkphp5.0.24 deserialization vulnerability
2022年深入推进IPv6部署和应用,该如何全面实现安全升级改造?
Basic principles of the Internet
字节二面:Redis主节点的Key已过期,但从节点依然读到过期数据是为什么?怎么解决?
npm install 命令的介绍
追更这个做嵌入式的大佬
Start from the principle of MVC and knock on an MVC framework to bring you the pleasure of being a great God
thinkphp5.0.24反序列化漏洞分析
原生JS动态添加和删除类
Kirin software and Geer software focus on the development of network data security
From in MySQL_ Unixtime and UNIX_ Timestamp processing database timestamp conversion - Case
10-2xxe vulnerability principle and case experiment demonstration
论文精读:Generative Adversarial Imitation Learning(生成对抗模仿学习)









