简体   繁体   English

在切换javascript时,Chrome应用样式表的速度非常慢

[英]Chrome is very slow to apply a style sheet when switching in javascript

I have a piece of javascript to switch a page's stylesheet on a button click. 我有一段JavaScript,可以在单击按钮时切换页面的样式表。 It works fine and zippy in IE and Firefox but it's very very slow in Chrome. 它在IE和Firefox中可以正常运行,但运行缓慢,但在Chrome中运行非常慢。 It takes about 30secs to re-render and that's on my development machine. 重新渲染大约需要30秒,这在我的开发机器上。 The load time is just a few ms. 加载时间仅为几毫秒。 I've tried this in angularjs using ng-href and binding the style sheet file name to a scope variable with poor results in Chrome. 我已经使用ng-href在angularjs中尝试了此操作,并将样式表文件名绑定到作用域变量,但在Chrome中效果不佳。 Then I included jquery and did it this way 然后我包括了jQuery,并以此方式

 var sheetName = "Content/RPNCalc-" + sizes[size] + ".css";
  angular.element("link[rel='stylesheet']").attr("href", sheetName);

again that works dandy in IE & FF but takes about 30 secs in Chrome. 再次在IE&FF中有效,但在Chrome中大约需要30秒。

What's the best way to do this? 最好的方法是什么?

Thanks 谢谢

In the end I did this as a directive called "active-sheet" and manipulated the "disabled" property. 最后,我作为名为“活动表”的指令执行了此操作,并操纵了“禁用”属性。 Works in IE FF & Chrome 适用于IE FF和Chrome

  <link rel="stylesheet" type="text/css" active-sheet="small" href="Content/RPNCalc-small.css" />
    <link rel="stylesheet" type="text/css" active-sheet="big" href="Content/RPNCalc-big.css" />

link: function (scope, element, attrs) {
            scope.$on("sizeClick", function (_e, _size) {
               element.prop("disabled", _size != attrs.activeSheet);
            });
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM