我的base.css和我的网站上有一个red.css.
当我按下某个按钮时,我喜欢为blue.css更改red.css,而不会丢失base.css如何做到这一点?我试过这个:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
<script type="text/javascript">
if($.cookie("css")) {
$("link").attr("href",$.cookie("css"));
}
$(document).ready(function(){
$("#troca_faccao").click(function() {
$(this).parent().find("#painel_faccao").slideDown('fast').show(); //Drop down the subnav on click
$(this).hover(function() {
}, function(){
$(this).parent().find("#painel_faccao").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
});
})
$("#painel_faccao li a").click(function() {
$("link").attr("href",$(this).attr('rel'));
$.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
return false;
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
和div:
<div id="painel_faccao" style="display: none">
<p>A Escolha é Sua!</p>
<ul>
<li class="horda"><a href="#" rel="horde.css">HORDA</a></li>
<li class="alianca"><a href="#" rel="aliance.css">ALIANÇA</a></li>
</ul></div>
Run Code Online (Sandbox Code Playgroud)
您可以使用:eq过滤器选择器或eq()方法来定位所需的样式表:
$("link:eq(0)").attr("href",$(this).attr('rel'));
Run Code Online (Sandbox Code Playgroud)
eq指定要更改的样式表的位置href。当然,您想更改href链接样式表指向red.css而不是base.css
或者,您可以将 应用于id要更改hrefofL 的链接样式表
<link id="someID"............/>
Run Code Online (Sandbox Code Playgroud)
然后再做:
$("link#someID").attr("href",$(this).attr('rel'));
Run Code Online (Sandbox Code Playgroud)