【问题标题】:Stylesheet switcher button to toggle html when clicked (jQuery)?单击时切换 html 的样式表切换器按钮(jQuery)?
【发布时间】:2015-03-09 09:45:16
【问题描述】:

我的页面顶部有一个按钮,可以使用单独的样式表进行打印,我有一个普通的 print.css,可以在打印时启用所有内容,我有一个 acrfprint.css,它只打印某些元素。上周有人给了我一些帮助,让我使用以下 JS 使其工作:

<script>
$(document).ready(function(){
    $('#acrf').click(function(){
        $('link').first().removeAttr('href').attr('href', '_includes/css/acrfprint.css');
    });
});
</script>

这很完美,但唯一的问题是,如果 acrf-print.css 已启用或如何禁用,该按钮并不能真正让您知道。

基本上我使用的 HTML 是这样的:

<div id='acrfContaineroff'>
<button id='acrf' class='noPrint noPrint-acrf'>Enable Annotation Print Mode</button>
</div>

(我知道我使用的是 ' 而不是 " 但那是因为它嵌套在 VBScript 中)

我想要做的是当点击按钮时,将样式表更改为 acrfprint.css ,然后改为显示以下 HTML

<div id='acrfContaineron'>
<button id='acrf' class='noPrint noPrint-acrf'>Disable Annotation Print Mode</button>
</div>

当禁用按钮被选中时,我希望 css 返回到 print.css

任何帮助将不胜感激。

【问题讨论】:

  • 你可以看看你自己以前的问题,以防你想链接到它,让大家更容易.. [stackoverflow.com/questions/28897149/…
  • 那么,你想切换样式表和按钮的文本吗?
  • @LShetty 是的,差不多,只是某种视觉帮助,将 print.css 更改为 printacrf.css,如果再次单击它,它会恢复为 print.css。跨度>
  • @DaveBurns 对。请参阅下面的我的答案。

标签: javascript jquery html css stylesheet


【解决方案1】:

由于您的问题看起来像是刚开始使用 javascript.. 也许您应该将按钮更改为复选框?

<div id='acrfContaineroff'>
<button id='acrf' class='noPrint noPrint-acrf'>Enable Annotation Print Mode</button>
</div>

进入:

<div id='acrfContaineroff'>
<input type='checkbox' id='acrf' /> Enable Annotation Print Mode
</div>

并将javascript放入:

<script>
$(document).ready(function(){
    $('#acrf').click(function(){
        if( $('#acrf').is(':checked') ){
            $('link').first().removeAttr('href').attr('href', '_includes/css/acrfprint.css');
        }else{
            $('link').first().removeAttr('href').attr('href', '_includes/css/print.css');
        }
    });
});
</script>

【讨论】:

  • 完美,这是我的第一个想法,但我不知道怎么做,你说得对,JS 对我来说很新:)
【解决方案2】:

因此,如果您在单击按钮时使用样式表切换器并相应地更改按钮标签,这可能会有所帮助。

假设:启用注释打印模式 = acrfprint.css

$('#acrf').click(function(){
    var label = "Disable Annotation Print Mode";
    var link = $('#myStylesheet');
    if (link.attr("href").indexOf("acrf") !== -1) {
        link.attr('href', '_includes/css/print.css');
        label = "Enable Annotation Print Mode";
    } else {
        link.attr('href', '_includes/css/acrfprint.css');
    }
    $(this).text(label);
});

HTML:

<div id='acrfContaineron'>
    <button id='acrf' class='noPrint noPrint-acrf'>Disable Annotation Print Mode</button>
</div>

<link id="myStylesheet" rel="stylesheet" type="text/css" href="_includes/css/acrfprint.css" />

演示@Fiddle

【讨论】:

  • 谢谢你,我试过了,它在一定程度上奏效了,但是复选框答案的人是我需要的最简单的方法,不过感谢你的时间!
  • 非常好,但我需要有 15 分才能这样做:(
【解决方案3】:

您可以使用以下自己创建的演示:
假设我们在 HEAD 部分定义了以下样式表。

<link rel="stylesheet" type="text/css" title="blue" href="http://example.com/css/blue.css">
<link rel="alternate stylesheet" type="text/css" title="pink" href="http://example.com/css/pink.css">

HTML 代码为您的用户提供一种选择样式表的方式

<form>
    <input type="submit" onclick="switch_style('blue');return false;" name="theme" value="Blue Theme" id="blue">
    <input type="submit" onclick="switch_style('pink');return false;" name="theme" value="Pink Theme" id="pink">
</form>

改变样式表的 JavaScript 函数

        // *** TO BE CUSTOMISED ***

    var style_cookie_name = "style" ;
    var style_cookie_duration = 30 ;

    // *** END OF CUSTOMISABLE SECTION ***
    // You do not need to customise anything below this line

    function switch_style ( css_title )
    {
        // You may use this script on your site free of charge provided
        // you do not remove this notice or the URL below. Script from
        // http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
        var i, link_tag ;
        for (i = 0, link_tag = document.getElementsByTagName("link") ;
          i < link_tag.length ; i++ ) {
            if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) &&
              link_tag[i].title) {
                link_tag[i].disabled = true ;
                if (link_tag[i].title == css_title) {
                    link_tag[i].disabled = false ;
                }
            }
            set_cookie( style_cookie_name, css_title,
              style_cookie_duration );
        }
    }
    function set_style_from_cookie()
    {
        var css_title = get_cookie( style_cookie_name );
        if (css_title.length) {
            switch_style( css_title );
        }
    }
    function set_cookie ( cookie_name, cookie_value,
        lifespan_in_days, valid_domain )
    {
        // http://www.thesitewizard.com/javascripts/cookies.shtml
        var domain_string = valid_domain ?
                           ("; domain=" + valid_domain) : '' ;
        document.cookie = cookie_name +
                           "=" + encodeURIComponent( cookie_value ) +
                           "; max-age=" + 60 * 60 *
                           24 * lifespan_in_days +
                           "; path=/" + domain_string ;
    }
    function get_cookie ( cookie_name )
    {
        // http://www.thesitewizard.com/javascripts/cookies.shtml
        var cookie_string = document.cookie ;
        if (cookie_string.length != 0) {
            var cookie_value = cookie_string.match (
                            '(^|;)[\s]*' +
                            cookie_name +
                            '=([^;]*)' );
            return decodeURIComponent ( cookie_value[2] ) ;
        }
        return '' ;
    }

您还需要将onload 属性添加到您的网页正文标记:

<body onload="set_style_from_cookie()">

【讨论】:

  • 抱歉,这与我想要实现的目标无关,我已经有另一个样式切换器,可以按照您的说法在页面主题之间切换。我也想避免使用表单按钮,因为不需要它们。最重要的是,我不需要/不想在这部分使用 cookie,我已经为主题切换器设置了 cookie,只是不想为 printacrf.css 使用 cookie,因为只有少数人需要临时使用它。不过感谢您的意见。
  • 恐怕不是,我写主题切换器的时候应该是这样的,而不是打印切换器:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
相关资源
最近更新 更多