当前位置:网站首页>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’
边栏推荐
- 0 basic learning C language - nixie tube dynamic scanning display
- Make drop-down menu
- 1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
- 求esp32C3板子连接mssql方法
- [common error] UART cannot receive data error
- GUI application: socket network chat room
- leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
- 删除所有值为y的元素。数组元素中的值和y的值由主函数通过键盘输入。
- mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
猜你喜欢
MySQL statement learning record
How to delete MySQL components using xshell7?
Hbuilder link Xiaoyao simulator
2-Redis架构设计到使用场景-四种部署运行模式(下)
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
How to set the response description information when the response parameter in swagger is Boolean or integer
Hash table, string hash (special KMP)
手机异步发送短信验证码解决方案-Celery+redis
Lightweight Pyramid Networks for Image Deraining
随机推荐
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
Lightweight Pyramid Networks for Image Deraining
Summary of common tools and technical points of PMP examination
0 basic learning C language - nixie tube dynamic scanning display
数据库表外键的设计
Hash table, string hash (special KMP)
手机异步发送短信验证码解决方案-Celery+redis
Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".
The FISCO bcos console calls the contract and reports an error does not exist
Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
Future source code view -juc series
Install the pit that the electron has stepped on
Some other configurations on Huawei's spanning tree
In the process of seeking human intelligent AI, meta bet on self supervised learning
Gee: create a new feature and set corresponding attributes
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
How programmers find girlfriends through blind dates
A little understanding of GSLB (global server load balance) technology
ThinkPHP uses redis to update database tables
Which insurance products can the elderly buy?