我正在将主题设置添加到我的 js 应用程序中。该应用程序也应该离线工作,所以我必须检查远程样式文件(例如http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/jquery/jquery-ui.css
) 存在并且可到达,否则我设置href
in <link rel="stylesheet">
指向本地文件。
到目前为止,我使用了以下代码,但使用 jQuery 2.1.3 时,它在控制台上输出警告:
主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。如需更多帮助,请检查http://xhr.spec.whatwg.org/ http://xhr.spec.whatwg.org/.
在线引出jquery-2.1.3.js:8556
好吧,我可以忽略这个警告......
url = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/jquery/jquery-ui.css";
$.ajax({url: url,
type: "HEAD",
timeout: 1000,
async: false,
error: function() {url = 'css/jquery-ui.css';}
});
/* modify link statement to use url for href */
在寻找替代方法时,我尝试检查链接语句在更改其 href 后是否已加载新的 css,例如:
$('#id_of_the_link_stmt').error(function() {
console.log("New url not loaded, reverting to local file")
}).attr('href', url);
但它不起作用。
感谢您的建议!
为什么不使用替代样式表和组?
您始终可以包含基本样式表和自定义样式表作为备用样式表:
<link rel="stylesheet" type="text/css" href="css/base.css" title="base" />
<link rel="alternate stylesheet" type="text/css" href="css/custom.css" title="base" />
在这种情况下,custom.css可用,浏览器将加载他(仅他) - 如果它不可用,浏览器将移至第一个(没有备用)并加载他 - 例如:
<link rel="stylesheet" type="text/css" href="css/base.css" title="base" />
<link rel="alternate stylesheet" type="text/css" href="css/custom11111.css" title="base" />
现在,base.css 已加载,因为 custom1111.css 不可用。
请注意,该组是由以下定义的title
属性 - 您也可以设置许多组 - 例如:
<link rel="stylesheet" type="text/css" href="css/main.css" title="base" />
<link rel="alternate stylesheet" type="text/css" href="css/main_custom.css" title="base" />
<link rel="stylesheet" type="text/css" href="css/nav.css" title="nav" />
<link rel="alternate stylesheet" type="text/css" href="css/nav_custom.css" title="nav" />
使用 JavaScript 切换样式表:
您可以通过将样式表设置为来禁用它disabled = true
使用 title 属性来标识要禁用或启用的正确样式表。
更多阅读和示例:
- 替代 CSS 样式/主题 http://www.thesitewizard.com/css/switch-alternate-css-styles.shtml
- 如何使用 JavaScript 动态更改级联样式表 (CSS) http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)