[英]How to change the css stylesheet in CSS and JS?
function colorChanger() {
var len = document.getElementById("string").value.length;
if (len < 50) {
style1.onclick = swapStyleSheet("first_50.css");
} else if (len > 50 && len < 100) {
style1.onclick = swapStyleSheet("second_100.css");
}
}
function swapStyleSheet(sheet){
document.getElementById('css_style').setAttribute('href', sheet);
}
这是我的代码,如果字符数less than 50
且more than 50 and less than 100
,我希望它更改样式表
我的代码不起作用
这是我的代码,它正在更改样式表。
<html>
<head>
<title>
Change Css
</title>
<script lang="javascript">
function colorChanger() {
var len = document.getElementById("string").value.length;
if (len < 50) {
swapStyleSheet("first_50.css");
} else if (len > 50 && len < 100) {
swapStyleSheet("second_100.css");
}
}
function swapStyleSheet(strName) {
document.getElementById("cssChanger").href = strName;
}
</script>
<link href="first_50.css" id="cssChanger" />
</head>
<body>
<input type="text" id="string" value=""></input>
<input type="button" id='style1' value="Change Css" onclick="javascritpt:colorChanger();">
</body>
</html> ````
function changeCSS(cssFile, cssLinkIndex) {
var oldlink = document.getElementsByTagName("link").item(cssLinkIndex);
var newlink = document.createElement("link");
newlink.setAttribute("rel", "stylesheet");
newlink.setAttribute("type", "text/css");
newlink.setAttribute("href", cssFile);
document.getElementsByTagName("head").item(0).replaceChild(newlink, oldlink);
}
function colorChanger() {
var len = document.getElementById("string").value.length;
if (len < 50) {
changeCSS("first_50.css",index); // index is head tag child element index ex:0
} else if (len > 50 && len < 100) {
changeCSS("second_100.css",index);// index is head tag child element index ex:0
}
}
好的,我已经弄清楚了,我不需要交换整个样式表,因此必须将 css 重新加载到 DOM 中。我可以使用 element.classList.remove('good-tier')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.