我目前正在尝试做一系列按钮,我的网站的用户可以点击,以改变不同的CSS3文件之间的变化,这将改变某些效果。为了实现这个目标,我需要一些方法来访问
href="example1.css"标记,并将其更改为
href="example2.css" 使用JavaScript或HTML。
发布于 2015-05-22 16:45:02
为您的链接分配一个id。通过其id在JS中获取它,并更改href属性。
<link rel="stylesheet" type="text/css" href="example1.css" id="lnk"/>在联合来文中:
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";发布于 2016-12-13 10:20:04
不
var link = document.getElementsById("lnk"); //Fetch the link by its ID 但
link = document.getElementById("lnk"); ID为1,getElementsById - is数组。
https://stackoverflow.com/questions/30401880
复制相似问题