如何使用 jQuery 切换 CSS 样式表?
我正在做的事情很简单。
您单击按钮 (id="themes")
,它会打开一个 div (id="themedrop")
,该 div 会向下滑动并列出主题。 (我现在只有两个)
<button id="original">Original</button><br />
<button id="grayscale">Grayscale</button>
现在当网站加载时,它会加载 style1.css (主/原始主题)
<link rel="stylesheet" type="text/css" href="style1.css">
现在我想弄清楚的是...... 当单击灰度按钮将样式表从 style1.css 更改为 style2.css (注意:文件位于同一目录中)时,我怎样才能做到这一点?
任何帮助将不胜感激。
What I'm working on is simple.
You click on a button (id="themes")
and it opens up a div (id="themedrop")
that slides down and lists the themes. (I only have two at this point)
<button id="original">Original</button><br />
<button id="grayscale">Grayscale</button>
Now when the site is loaded it loads with style1.css (main/original theme)
<link rel="stylesheet" type="text/css" href="style1.css">
Now what I'm trying to figure out is...
How can I have it that when the grayscale button is clicked to change the stylesheet from style1.css to style2.css (note: files are in the same directory)
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试一下,但不确定它是否有效,我还没有测试过,但祝你好运。
Give this a try but not sure if it will work I have not tested it but gd luck.
我建议您为
link
标签提供一个 id,例如主题。将 css 文件的名称放在按钮上的data
属性中,并对它们使用相同的处理程序:Html:
和 js:
I would suggest you give the
link
-tag an id such as theme. Put the name of the css file in adata
-attribute on the buttons and use the same handler on them both:Html:
And js:
您可以尝试通过以下方式做到这一点:
You can try to do that by this way:
使用这个:
Use this :
我的网页默认有一个深色主题。为了将其转换为浅色主题,我使用了谷歌的太阳图标字体。简单来说,jquery 中切换主题的代码是:
if 和 else if 语句的第一行是完整的答案。其余的文本仅涉及单击时将图标从月亮更改为太阳,反之亦然。
I had a dark theme by default on my webpage. To convert it to light theme, I had used the sun icon font by google. So in a simple way, the code to switch between the themes in jquery was:
The first line of the if and else if statement is the full answer. The remaining text involves just changing the icon when clicked from moon to sun, or vice-versa.