【问题标题】:CSS style switcher disabled in Safari 5.1Safari 5.1 中禁用了 CSS 样式切换器
【发布时间】:2011-12-11 09:43:07
【问题描述】:

我的网页中有这个基于 jQuery 的 CSS 样式切换器。当我将浏览器更新到 Safari 5.1 时,此脚本停止工作。我环顾四周,看到this 苹果讨论页面,原因是 Safari 5.1 中禁用了备用样式表。谁能帮我解决这个问题?我不是程序员,所以我无法弄清楚。

作者页面http://www.kelvinluck.com/assets/jquery/styleswitch/toggle.html

(function($)
{
    $(document).ready(function() {
        $('.styleswitch').click(function()
        {
            switchStylestyle(this.getAttribute("rel"));
            return false;
        });
        var c = readCookie('style');
        if (c) switchStylestyle(c);
    });

    function switchStylestyle(styleName)
    {
        $('link[rel*=style][title]').each(function(i) 
        {
            this.disabled = true;
            if (this.getAttribute('title') == styleName) this.disabled = false;
        });
        createCookie('style', styleName, 365);
    }
})(jQuery);

【问题讨论】:

    标签: javascript jquery css safari


    【解决方案1】:

    在搜索 SO 和网络后,我对任何解决方案都不满意。所以我想出了一个新的解决方案,它可以在旧 ipad 上使用 chrome、ff、ie 和 safari 和 safari:

    第一组样式:

    <link rel="stylesheet"              href="./codebase/touchui.css"       data-title="default"        type="text/css" media="screen" charset="utf-8">
    <link rel="alternate stylesheet"    href="./codebase/ios.css"           data-title="ios"                type="text/css" media="screen" charset="utf-8">
    <link rel="alternate stylesheet"    href="./codebase/jq.css"            data-title="jq"                 type="text/css" media="screen" charset="utf-8">
    <link rel="alternate stylesheet"    href="./codebase/sky.css"           data-title="sky"                type="text/css" media="screen" charset="utf-8">
    <link rel="alternate stylesheet"    href="./codebase/green.css"         data-title="green"          type="text/css" media="screen" charset="utf-8">
    

    注意属性“data-title”这是一个用户定义的属性。

    然后用这个函数来改变样式表,(注意我是在app范围内设置的,你可以把它设为标准函数:

    app = {}
    
    app.styleSet=function(title) {
      var i, a
      var o;
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute('data-title') ) {
          if (a.getAttribute('data-title') == title) 
            o = a
    
          a.setAttribute("rel", "alternate stylesheet");          // the order here is important
          a.disabled = true
          a.setAttribute("title", a.getAttribute('data-title'));
      }
    
      o.setAttribute("title", undefined);                         // the order here is important
      o.disabled = false
      o.setAttribute("rel", "stylesheet"); 
      //app.cookieCreate("style", title, 365);
    }
    

    【讨论】:

      【解决方案2】:

      我刚刚遇到了同样的问题。我的猜测是 Safari 不喜欢禁用样式表的 标签,所以我稍微修改了代码。

      现在它将只使用一个 标记,并根据您单击的任何内容的 rel 属性替换 href 属性:

      (function($)
      {
        $(document).ready(function() {
          $('.styleswitch').click(function() {
            switchStylestyle(this.getAttribute("rel"));
            return false;
          });
          var c = readCookie('style');
          if (c) switchStylestyle(c);
        });
      
        function switchStylestyle(styleName)
        {
          linktag = $('link[rel*=style][title=theme]');
          linktag.attr('href', styleName);
          createCookie('style', styleName, 365);
        }
      })(jQuery);
      

      现在要使用它,你需要添加一个 标签,标题为“theme”,指向你的默认主题:

      <link rel="stylesheet" type="text/css" title="theme" href="light.css"/>
      

      当您单击具有“styleswitch”类的链接时,它会将“主题” 的 href 属性设置为被单击的 标记的“rel”属性:

      <a href="#" rel="dark.css" class="styleswitch">Switch to dark stylesheet</a>
      

      【讨论】:

      • 谢谢!由于我没有使用 jQuery,所以我使用它来设置 href 属性:linktag.setAttribute("href", "../style_new.css");
      【解决方案3】:

      找到了一个适用于 Safari 5 的 jQuery 脚本的链接。

      http://electrokami.com/coding/jquery-style-sheet-switcher-with-preview/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 2015-12-01
        • 1970-01-01
        相关资源
        最近更新 更多