当前位置:网站首页>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’
边栏推荐
- ThinkPHP uses redis to update database tables
- C import Xls data method summary V (complete code)
- String hash, find the string hash value after deleting any character, double hash
- PMP 考试常见工具与技术点总结
- It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
- Long article review: entropy, free energy, symmetry and dynamics in the brain
- Gauss elimination method and template code
- Windos10 reinstallation system tutorial
- @EnableAsync @Async
- About uintptr_ T and IntPtr_ T type
猜你喜欢

How to use AHAS to ensure the stability of Web services?

How to delete MySQL components using xshell7?

Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
![[dynamic programming] leetcode 53: maximum subarray sum](/img/f0/80357f9ffc556f3ed4d3aa0901bb1d.jpg)
[dynamic programming] leetcode 53: maximum subarray sum

Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?

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

In the process of seeking human intelligent AI, meta bet on self supervised learning

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

基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
随机推荐
All in one 1407: stupid monkey
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
1-Redis架构设计到使用场景-四种部署运行模式(上)
[common error] custom IP instantiation error
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
C import Xls data method summary III (processing data in datatable)
Gauss elimination method and template code
AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
2020-12-02 SSM advanced integration Shang Silicon Valley
QML add gradient animation during state transition
Long article review: entropy, free energy, symmetry and dynamics in the brain
Oracle database knowledge points (I)
Future源码一观-JUC系列
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
On covariance of array and wildcard of generic type
Who moved my code!
Make drop-down menu