且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据用户语言在angular2应用程序中动态加载不同的CSS文件

更新时间:2023-11-26 21:26:58

您需要有条件地从浏览器中添加或删除样式:

You'd need to conditionally add or remove the style from the browser:

添加

loadIfNot(url,cssId){

     if (!document.getElementById(cssId)){
        var head  = document.getElementsByTagName('head')[0];
        var link  = document.createElement('link');
        link.id   = cssId;
        link.rel  = 'stylesheet';
        link.type = 'text/css';
        link.href = url;
        link.media = 'all';
        head.appendChild(link);
    }else{
        document.getElementById(cssId).disabled = false;///i fit's already there, enable it
    }

  }

并删除

 disable(cssId){
     document.getElementById(cssId).disabled = true
 }

DisableEnable是因为浏览器倾向于缓存您的样式,并且为了使它们启用或禁用,您需要更改该属性

Disable and Enable is because browser tends to cache your styles, and in order to make them enable or disable, you'd change that attribute