I'm currently trying to make a series of buttons that the user of my site can click on in order to change between different CSS3 files, which will change certain effects. In order to accomplish this goal, I need some way of accessing the
href="example1.css"
tag in my HTML, and changing it to
href="example2.css"
using JavaScript or HTML.
Assign an id
to your link. Fetch it in JS by its id
and change the href
attribute.
<link rel="stylesheet" type="text/css" href="example1.css" id="lnk"/>
In JS:
var link = document.getElementsById("lnk"); //Fetch the link by its ID
link.setAttribute("href", "example2.css"); //Change its href attribute
You can also do it without an id
.
document.querySelector("link[href='example1.css']").href = "example2.css";
not
var link = document.getElementsById("lnk"); //Fetch the link by its ID
but
link = document.getElementById("lnk");
The ID is ONE, getElementsById - is array
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.