当前位置:网站首页>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’
边栏推荐
- 7.1 学习内容
- 关于 uintptr_t和intptr_t 类型
- How to use AHAS to ensure the stability of Web services?
- The super fully automated test learning materials sorted out after a long talk with a Tencent eight year old test all night! (full of dry goods
- 0 basic learning C language - nixie tube dynamic scanning display
- Since the "epidemic", we have adhered to the "no closing" of data middle office services
- Gnupg website
- QML add gradient animation during state transition
- Lightweight Pyramid Networks for Image Deraining
- Swagger2 quick start and use
猜你喜欢
AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
How programmers find girlfriends through blind dates
[prefix and notes] prefix and introduction and use
中电资讯-信贷业务数字化转型如何从星空到指尖?
Some other configurations on Huawei's spanning tree
Infiltration learning diary day19
swagger中响应参数为Boolean或是integer如何设置响应描述信息
How can enterprises optimize the best cost of cloud computing?
Fundamentals of machine learning: feature selection with lasso
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
随机推荐
Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
All in one 1407: stupid monkey
How programmers find girlfriends through blind dates
Summary of JWT related knowledge
0 basic learning C language - nixie tube dynamic scanning display
ThinkPHP uses redis to update database tables
长文综述:大脑中的熵、自由能、对称性和动力学
“疫”起坚守 保障数据中台服务“不打烊”
2-Redis架构设计到使用场景-四种部署运行模式(下)
How to delete MySQL components using xshell7?
The FISCO bcos console calls the contract and reports an error does not exist
打印菱形图案
51 MCU external interrupt
Luogu p1309 Swiss wheel
MySQL deadly serial question 2 -- are you familiar with MySQL index?
Gee: create a new feature and set corresponding attributes
[dynamic programming] leetcode 53: maximum subarray sum
Introduction to A-frame virtual reality development
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
CLP information - how does the digital transformation of credit business change from star to finger?