使用Javascript在多个CSS文件之间切换 [英] Switching between multiple CSS files using Javascript

查看:79
本文介绍了使用Javascript在多个CSS文件之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一系列按钮,我的网站用户可以点击这些按钮,以便在不同的CSS3文件之间切换,这将改变某些效果。为了实现这个目标,我需要一些方法来访问

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"

我的HTML中的

标记,并将其更改为

tag in my HTML, and changing it to

href="example2.css" 

使用JavaScript或HTML。

using JavaScript or HTML.

推荐答案

为您的链接分配 id 。通过其 id 在JS中获取它,并更改 href 属性。

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"/>

在JS中:

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

您也可以在没有 id <的情况下执行此操作/ code>。

You can also do it without an id.

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

这篇关于使用Javascript在多个CSS文件之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆