【问题标题】:How to add CSS code for multiple color in one click on web page如何在网页上一键添加多种颜色的 CSS 代码
【发布时间】:2022-02-11 15:22:43
【问题描述】:
【问题讨论】:
标签:
javascript
php
html
css
responsive-design
【解决方案2】:
“我在CSS或网站的一个地方定义了颜色代码,然后整个网站都应该根据它进行更改。”
以上,是网页动态颜色的最佳想法。
示例代码:-
function change(color){
$("body").removeClass();
$("body").addClass(color);
}
body.black {
background-color : black;
color: white;
}
body.black .heading_1{
font-size: 30px;
font-weight : bold;
}
body.red {
background-color : red;
color: white;
}
body.red .heading_2{
font-size: 30px;
font-weight : bold;
}
body.green {
background-color : green;
color: white;
}
body.green .heading_3{
font-size: 30px;
font-weight : bold;
}
body.blue {
background-color : blue;
color: white;
}
body.blue .heading_4{
font-size: 30px;
font-weight : bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body class="black">
<span class="heading_1"> This is Black </span><br/>
<span class="heading_2"> This is Red </span><br/>
<span class="heading_3"> This is Green </span><br/>
<span class="heading_4"> This is Blue </span><br/>
<br/>
<button onclick="change('black')"> Black </button>
<button onclick="change('red')"> Red </button>
<button onclick="change('green')"> Green </button>
<button onclick="change('blue')"> Blue </button>
</body>
</html>
您可以为页面上的任何元素 (I have it on heading in my case) 定义任何样式,也可以为页面上要更改样式的任何元素 (body.<class> in my case) 定义任何类对于其他元素。