<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./test.css" id="cssFile">
<title>Document</title>
</head>
<body>
<h3>change Style</h3>
<div onclick="changeStyle('testa')">
testa
</div>
<div onclick="changeStyle('testb')">
testB
</div>
<script>
function changeStyle(type){
let css = document.getElementById('cssFile')
css.href = `./${type}.css`
}
</script>
</body>
</html>
三个css 文件
// test
html div{
display: inline-block;
margin: 20px;
width: 200px;
border: 1px solid black;
color: black;
}
//testa
html div{
display: inline-block;
margin: 20px;
width: 200px;
border: 1px solid red;
color: red;
}
//testb
html div{
display: inline-block;
margin: 20px;
width: 200px;
border: 1px solid blue;
color: blue;
}
可以的话,点个采纳
这里改一下:css.href = 'css/' + name + '.css'