如何使用Jquery更改辅助样式表? [英] How to change secondary stylesheet with Jquery?

查看:45
本文介绍了如何使用Jquery更改辅助样式表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有我的base.css和red.css.

I have my base.css and a red.css on my site.

我喜欢在按下某个按钮时将red.css更改为blue.css,而不会丢失base.css怎么办?我已经尝试过了:

I like do change red.css for blue.css when i press some button, without lost base.css how to do this? i have tried this:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>

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

和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>

推荐答案

您可以使用:eq过滤器选择器或eq()方法来定位所需的样式表:

You can use :eq filter selector or eq() method to target the stylesheet you want:

$("link:eq(0)").attr("href",$(this).attr('rel'));

将要更改href的样式表的位置指定为eq.当然,您想更改链接样式表的href指向red.css而不是base.css

Specify the position to eq for the stylesheet you want to change the href. Off course you want to change the href of link stylesheet pointing to red.css and not base.css

或者,您可以将id应用于要更改L的href的链接样式表

Alternatively, you can apply an id to the link stylesheet you want to change the href ofL

<link id="someID"............/>

然后做:

$("link#someID").attr("href",$(this).attr('rel'));

这篇关于如何使用Jquery更改辅助样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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