[英]using jquery load diffent css?
我有两个按钮。 现在我希望访问者单击按钮 2,当单击按钮 1 时,该站点会调用 style2.ZC7A62 style2.css
。 调用style.css
。 默认显示style.css
有没有办法使用 jquery 更改 css 的链接路径。
<link rel="stylesheet" href="http://www.example.com/themes/style.css" type="text/css"/>
如果单击 button2,将行更改为 < link rel="stylesheet" href="http://www.example.com/themes/style2.css" type="text/css"/>
或其他获得效果的方法。 谢谢你
在 button2 的 onclick 处理程序中,执行以下操作:
$('link').attr('href', 'http://www.example.com/themes/style2.css');
编辑:显然我需要指出,如果您有多个链接标签,这将影响所有链接标签,因此如果您有多个链接,请使用 id 作为选择器,例如:
<link id="myLink".../>
$('#myLink).attr('href',...);
您可以让 jQuery 更改/添加 class 到您的<body>
元素。 这确实意味着您必须在一个样式表中包含所有 styles。
例如:
CSS:
.some-style { color: #f00; }
.blue .some-style { color: #00f; }
.green .some-style { color: #0f0; }
HTML:
<html>
<body>
<p class="some-style">Some text</p>
<button id="btn-1">Blue</button>
<button id="btn-2">Green</button>
</body>
</html>
Javascript(为示例目的而简化):
$('#btn-1').click(function() {
$('body').addClass('blue');
});
$('#btn-2').click(function() {
$('body').addClass('green');
});
尝试使用您的 php 代码设置条件,如下所示:
<?if(buttonOneClicked()){?>
<link rel="stylesheet" href="http://www.example.com/themes/style.css" type="text/css"/>
<?}else{?>
<link rel="stylesheet" href="http://www.example.com/themes/style2.css" type="text/css"/>
<?}?>
关于这个基本想法的很好的文章:
http://www.rickardnilsson.net/post/2008/08/02/Applying-stylesheets-dynamically-with-jQuery.aspx
If you find you cannot remove a css file, you may eed to have your buttons redirect with a url variable your jquery can read and attach the appropriate css file.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.