首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用jquery切换样式表

使用jquery切换样式表
EN

Stack Overflow用户
提问于 2015-10-22 01:49:35
回答 4查看 493关注 0票数 0

我需要在jquery中找到正确的代码,这样我就可以在样式表之间快速切换按钮。我目前没有jquery,因为我所有的都失败了。我还在学呢。总之,这里是html:

代码语言:javascript
复制
/* Theme 1 */
body {
	background: #ff4455;
}
/* Theme 2 */
body {
	background: #ee7744;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<body>
	<input type="button" id="theme1">
	<input type="button" id="theme2">

	<script type="text/javascript"></script>
</body>

下面是代码的一个片段。我需要<script type="text/javascript"></script>内部的jquery

谢谢你的帮助!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-10-22 02:12:09

尝试使用style元素,并将type设置为text/plain;单击input type="button" set style type="text/css"以文本形式显示classinput type="button"id相同的内容。方法维护同一文档中的所有css,无需在样式的每个切换处请求外部样式表。

代码语言:javascript
复制
$("input[type=button]").click(function() {
  $("[type$=css]").text($("." + this.id).text())
})
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<style type="text/css">
</style>
<style type="text/plain" class="theme1">
  /* Theme 1 */ 
  body { background: #ff4455; }
</style>
<style type="text/plain" class="theme2">
  /* Theme 2 */ 
  body { background: #ee7744; }
</style>

<body>
  <input type="button" id="theme1" value="theme1">
  <input type="button" id="theme2" value="theme2">
</body>

票数 0
EN

Stack Overflow用户

发布于 2015-10-22 01:59:41

你也有同样的问题,为什么不花点时间研究一下。

他们使用jquery将链接的属性值更改为样式表。

How do I switch my CSS stylesheet using jQuery?

谢谢。

票数 1
EN

Stack Overflow用户

发布于 2015-10-22 02:05:09

应该有许多类似的问题和答案,这里有一个无论如何。

代码语言:javascript
复制
<html>
<head>
    <link href="css/sheet1.css" rel="stylesheet" type="text/css" id="sheet1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
    <input type="button" id="theme1">
    <input type="button" id="theme2">
    <script type="text/javascript">
    $(document).ready(function () {
        $('#theme1').click(function(){
            $('#sheet2').remove();
            $('head').append('<link href="css/sheet1.css" rel="stylesheet" id="sheet1" />');
        });
        $('#theme2').click(function(){
            $('#sheet1').remove();
            $('head').append('<link href="css/sheet1.css" rel="stylesheet" id="sheet2" />');
        });
    });
    </script>
</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33272156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档