2015-05-22 130 views
2

我正在尝试制作一系列按钮,我的网站用户可以点击以在不同的CSS3文件之间进行切换,这将改变某些特效。为了实现这个目标,我需要使用JavaScript或HTML访问我的HTML的使用Javascript在多个CSS文件之间切换

href="example1.css" 

标签,并改变它的一些方法来

href="example2.css" 

+0

你可以这样做两种方式:使用香草JS,jQuery的或。这个问题被问及在这里回答:http://stackoverflow.com/questions/11522545/how-to-change-the-css-stylesheet-dynamically-for-a-website-being-viewed – SeanOlson

回答

6

指定一个id到您的链接。在JS中取其id并更改href属性。

<link rel="stylesheet" type="text/css" href="example1.css" id="lnk"/> 

在JS:

var link = document.getElementsById("lnk"); //Fetch the link by its ID 
link.setAttribute("href", "example2.css"); //Change its href attribute 

你也可以做到这一点没有id

document.querySelector("link[href='example1.css']").href = "example2.css"; 
0

var link = document.getElementsById("lnk"); //Fetch the link by its ID 

link = document.getElementById("lnk"); 

ID是ONE,getElementsById - 是数组