当前位置:网站首页>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’
边栏推荐
- Day05 table
- Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
- be based on. NETCORE development blog project starblog - (14) realize theme switching function
- Analysis and solution of lazyinitializationexception
- Who moved my code!
- The force deduction method summarizes the single elements in the 540 ordered array
- OS interrupt mechanism and interrupt handler
- 使用dnSpy对无源码EXE或DLL进行反编译并且修改
- MySQL statement learning record
- 1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
猜你喜欢

Pyinstaller packaging py script warning:lib not found and other related issues
![[dynamic programming] leetcode 53: maximum subarray sum](/img/f0/80357f9ffc556f3ed4d3aa0901bb1d.jpg)
[dynamic programming] leetcode 53: maximum subarray sum

Solution of cursor thickening

Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".

I don't care about you. OKR or KPI, PPT is easy for you

be based on. NETCORE development blog project starblog - (14) realize theme switching function

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

Since the "epidemic", we have adhered to the "no closing" of data middle office services
![[common error] custom IP instantiation error](/img/de/d3f90cd224274d87fcf153bb9244d7.jpg)
[common error] custom IP instantiation error

Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
随机推荐
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
Swagger2 quick start and use
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Audio resource settings for U3D resource management
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
51 single chip microcomputer timer 2 is used as serial port
AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
Long article review: entropy, free energy, symmetry and dynamics in the brain
HackTheBox-baby breaking grad
Technical practice online fault analysis and solutions (Part 1)
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
Delete all elements with a value of Y. The values of array elements and y are entered by the main function through the keyboard.
Stringutils and collectionutils
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
Typescript basic knowledge sorting
使用dnSpy对无源码EXE或DLL进行反编译并且修改
Future源码一观-JUC系列
be based on. NETCORE development blog project starblog - (14) realize theme switching function
All in one 1407: stupid monkey
Force deduction solution summary 1189- maximum number of "balloons"