当前位置:网站首页>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’
边栏推荐
- Long article review: entropy, free energy, symmetry and dynamics in the brain
- Future源码一观-JUC系列
- 机器学习基础:用 Lasso 做特征选择
- Characteristics of ginger
- C import Xls data method summary IV (upload file de duplication and database data De duplication)
- Who moved my code!
- It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
- Swagger2 quick start and use
- A malware detection method for checking PLC system using satisfiability modulus theoretical model
- TP5 automatic registration hook mechanism hook extension, with a complete case
猜你喜欢

我管你什么okr还是kpi,PPT轻松交给你

A-Frame虚拟现实开发入门

CLP information - how does the digital transformation of credit business change from star to finger?

Thinkphp6 integrated JWT method and detailed explanation of generation, removal and destruction

How to set the response description information when the response parameter in swagger is Boolean or integer

技术实践|线上故障分析及解决方法(上)

51 MCU external interrupt

Network layer - routing

Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL

Analysis and solution of lazyinitializationexception
随机推荐
Solution of cursor thickening
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程
A little understanding of GSLB (global server load balance) technology
TP5 automatic registration hook mechanism hook extension, with a complete case
C import Xls data method summary III (processing data in datatable)
How can enterprises optimize the best cost of cloud computing?
1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
The force deduction method summarizes the single elements in the 540 ordered array
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
Day05 table
打印菱形图案
Functions and arrays of shell scripts
Future source code view -juc series
CLP information - how does the digital transformation of credit business change from star to finger?
技術實踐|線上故障分析及解决方法(上)
How to use AHAS to ensure the stability of Web services?
gslb(global server load balance)技术的一点理解
leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
Make drop-down menu
中电资讯-信贷业务数字化转型如何从星空到指尖?