如何使用Javascript更改附加的CSS文件? [英] How can you change the attached CSS file with Javascript?
本文介绍了如何使用Javascript更改附加的CSS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道,是否可以使用JavaScript更改附件样式表?
例如:
< link href ="stylesheet.css" rel ="stylesheet" type ="text/css">.....< div id ="controls">< div id ="red" onClick ="styler(1)"> R</div>< div id ="green" onClick ="styler(2)"> G</div>< div id ="blue" onClick ="styler(3)"> B</div>< div id ="reset" onClick ="styler(4)">重置</div></div>
JavaScript
函数样式器(attr){开关(attr){case'1':document .----- ="stylesheet1.css"; break;case'2':document .----- ="stylesheet2.css"; break;case'3':document .----- ="stylesheet3.css"; break;case'4':document .----- ="stylesheet.css"; break;默认值:; break;}}
解决方案
向链接标记添加ID并使用
< link id ="myStyleSheet" href ="stylesheet.css" rel ="stylesheet" type ="text/css"/>< script type ="text/javascript">函数样式器(attr){var href;开关(attr){case'1':href ="stylesheet1.css"; break;case'2':href ="stylesheet2.css"; break;case'3':href ="stylesheet3.css"; break;case'4':href ="stylesheet.css"; break;默认值:; break;}document.getElementById('myStyleSheet').href = href;}</script>
参见此帖子 >
I was wondering, is it possible to change the attached stylesheet using JavaScript?
For example:
<link href="stylesheet.css" rel="stylesheet" type="text/css">
.....
<div id="controls">
<div id="red" onClick="styler(1)">R</div>
<div id="green" onClick="styler(2)">G</div>
<div id="blue" onClick="styler(3)">B</div>
<div id="reset" onClick="styler(4)">Reset</div>
</div>
JavaScript
function styler(attr){
switch(attr){
case'1':document.----- = "stylesheet1.css";break;
case'2':document.----- = "stylesheet2.css";break;
case'3':document.----- = "stylesheet3.css";break;
case'4':document.----- = "stylesheet.css";break;
default:;break;
}
}
解决方案
Add an id to the link tag and use
<link id="myStyleSheet" href="stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function styler(attr){
var href;
switch(attr){
case'1':href = "stylesheet1.css";break;
case'2':href = "stylesheet2.css";break;
case'3':href = "stylesheet3.css";break;
case'4':href = "stylesheet.css";break;
default:;break;
}
document.getElementById('myStyleSheet').href = href;
}
</script>
See this post
这篇关于如何使用Javascript更改附加的CSS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文