日期:2014-05-16  浏览次数:20376 次

请问 :javascript 如何动态改变外部样式链接
比如在一个HTML页面有这个外部样式链接:
<link   type= "text/css "   rel= "stylesheet "   href= "css/main.css "   />
现在我想通过javascript动态更换成另一个:
<link   type= "text/css "   rel= "stylesheet "   href= "css/main2.css "   />

该   如何实现??
请高手赐教!!谢谢了~~

------解决方案--------------------
<script>
if(){
css= 'main.css ';
}else{
css= 'main2.css ';
}
document.write ' <link type= "text/css " rel= "stylesheet " href= "css/ '+css+ ' " />
'
</script>


------解决方案--------------------
<link type= "text/css " id= "xx " rel= "stylesheet " href= "css/main.css " />

xx.setAttribute( "href ", "css/main2.css ")

------解决方案--------------------
另外一种方式

<html>
<head>
<link href= "stylesheet1.css " type= "text/css " rel= "stylesheet " id= "mycss ">
<script language= "javascript ">
var turnback=false;
function setCSS()
{
mycss.styleSheet.cssText= " ";
mycss.styleSheet.addImport(turnback? "stylesheet1.css ": "stylesheet2.css ");
turnback=!turnback;
}
</script>
</head>
<body>
<button onclick= "setCSS() " type= "button "> 切换背景颜色 </button>
</body>
</html>


StyleSheet1.CSS样式代码: BODY { background-color: Black; }

StyleSheet2.CSS样式代码:BODY { background-color: maroon; }