Safari 5.1中禁用了CSS样式切换器

我在我的网页上有这个基于jQuery的CSS样式切换器。 当我将浏览器更新到Safari 5.1时,此脚本停止工作。 我环顾四周,看到了这个苹果讨论页面,原因是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); 

找到了一个与Safari 5一起使用的jQuery脚本的链接。

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

我刚遇到同样的问题。 我的猜测是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”:

  

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

 Switch to dark stylesheet 

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

首套款式:

      

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

然后使用此函数更改样式表(请注意,我已将其设置在应用程序范围中,您可以将其设置为标准函数:

 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); }