当前位置:网站首页>Use classname to modify style properties
Use classname to modify style properties
2022-07-04 01:24:00 【How's Kakashi doing】
There are two ways to modify style attributes
The first one is : Inline style operation ,element.style , It is recommended to use when there are few styles that need to be changed or when the function is simple
The second kind : Class name style operation , element.className , When the styles that need to be modified are complex and many , Use this method
element.style
Inline style operation , It is recommended to use when there are few styles that need to be changed or when the function is simple , Directly in script Add... To the tag , Only one attribute style can be changed at a time , When there are many styles that need to be changed , This method is cumbersome .
<style>
div{
width: 200px;
height: 300px;
background-color: rgb(253, 180, 180);
}
</style>
</head>
<body>
<div></div>
<script>
var div1=document.querySelector('div');
div1.onclick=function(){
this.style.backgroundColor='#333' //element.style Mode change style
}
</script>
</body>element.className
When there are many attribute styles that need to be changed , This method is relatively simple , Add the style you want to show after changing directly in the way of class selector css Of style In the label , And then in script In the event element.className that will do , It should be noted that after the style changes , If there is no class name in the label , Then a class name will be added after the event is executed ( Just now, css Added in ), If there was Class name , It's going to overwrite
<style>
div{
width: 200px;
height: 300px;
background-color: rgb(253, 180, 180);
}
.change{
width: 200px;
height: 300px;
background-color: rgb(39, 89, 255);
}
</style>
</head>
<body>
<div></div>
<script>
var div1=document.querySelector('div');
div1.onclick=function(){
this.className='change'; // There is no dot before the class name here
}
</script>
</body>Before clicking :

After clicking :
After clicking div There is an extra class name in the tag , And the class name is the added changed class selector style
![]()
If you don't want to overwrite the previous style and want to change and add a new style , Use Multi class name selector
element.className=‘first,change’
边栏推荐
- CLP information - how does the digital transformation of credit business change from star to finger?
- 51 MCU external interrupt
- ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
- TP5 automatic registration hook mechanism hook extension, with a complete case
- PMP 考试常见工具与技术点总结
- Technical practice online fault analysis and solutions (Part 1)
- 技术实践|线上故障分析及解决方法(上)
- MySQL -- Introduction and use of single line functions
- 删除所有值为y的元素。数组元素中的值和y的值由主函数通过键盘输入。
- The force deduction method summarizes the single elements in the 540 ordered array
猜你喜欢

Solution of cursor thickening

Release and visualization of related data

Lightweight Pyramid Networks for Image Deraining

机器学习基础:用 Lasso 做特征选择

Windos10 reinstallation system tutorial

2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)

在寻求人类智能AI的过程中,Meta将赌注押向了自监督学习

1-Redis架构设计到使用场景-四种部署运行模式(上)

Weekly open source project recommendation plan

Unity Shader入门精要读书笔记 第三章 Unity Shader基础
随机推荐
Employees' turnover intention is under the control of the company. After the dispute, the monitoring system developer quietly removed the relevant services
I don't care about you. OKR or KPI, PPT is easy for you
Avoid playing with super high conversion rate in material minefields
The first training of wechat applet
C import Xls data method summary I (upload files and create Workbooks)
求esp32C3板子连接mssql方法
How can enterprises optimize the best cost of cloud computing?
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
A-Frame虚拟现实开发入门
Fundamentals of machine learning: feature selection with lasso
Summary of JWT related knowledge
Idsia & supsi & usi | continuous control behavior learning and adaptive robot operation based on Reinforcement Learning
机器学习基础:用 Lasso 做特征选择
Stringutils and collectionutils
[turn] solve the problem of "RSA public key not find" appearing in Navicat premium 15 registration
2020-12-02 SSM advanced integration Shang Silicon Valley
How to set the response description information when the response parameter in swagger is Boolean or integer
Function: write function fun to find s=1^k+2^k +3^k ++ The value of n^k, (the cumulative sum of the K power of 1 to the K power of n).
Mongodb learning notes: command line tools
Audio resource settings for U3D resource management