如何使用jQuery异步加载CSS?

Ada*_*zyk 20 css jquery asynchronous jquery-ui

我想使用jQuery异步加载文档的CSS.

我找到了这个示例,但这似乎在IE中不起作用:

<script type="text/javascript" src="/inc/body/jquery/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
    $(function(){
        $('<link>', {
            rel: 'stylesheet',
            type: 'text/css',
            href: '/inc/body/jquery/css/start/jquery-ui-1.8.10.custom.css'
        }).appendTo('head');
        $.getScript("/inc/body/jquery/js/jquery-ui-1.8.10.custom.min.js", function(){
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

基本上我想加载jQuery UI所有其他数据,图像,样式等加载到页面.

这个$.getScript作品非常适合JS文件.唯一的问题是IE中的CSS.

你知道更好的解决方案吗?

Dmi*_*ich 42

这是一个异步样式表加载的方法,它不会阻止对我有用的页面呈现:

https://github.com/filamentgroup/loadCSS/blob/master/loadCSS.js

根据lonesomeday的答案减少了上面链接的代码示例

var stylesheet = document.createElement('link');
stylesheet.href = '/inc/body/jquery/css/start/jquery-ui-1.8.10.custom.css';
stylesheet.rel = 'stylesheet';
stylesheet.type = 'text/css';
// temporarily set media to something inapplicable to ensure it'll fetch without blocking render
stylesheet.media = 'only x';
// set the media back when the stylesheet loads
stylesheet.onload = function() {stylesheet.media = 'all'}
document.getElementsByTagName('head')[0].appendChild(stylesheet);
Run Code Online (Sandbox Code Playgroud)


lon*_*day 31

安全的方式是旧的方式......

var stylesheet = document.createElement('link');
stylesheet.href = '/inc/body/jquery/css/start/jquery-ui-1.8.10.custom.css';
stylesheet.rel = 'stylesheet';
stylesheet.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(stylesheet);
Run Code Online (Sandbox Code Playgroud)

  • 为什么这比jQuery更可取?你提到这是"安全"的方式.是什么让它比接受的答案更安全(假设当然存在jQuery)? (5认同)
  • 让我告诉你为什么这种方法比jQuery更有效...因为如果你通过jQuery加载它,你将不得不等到jQuery加载但是使用这种方法你不需要等待jQuery而你可以加载jQuery,CSS和其他脚本并行 (5认同)
  • 根据经验:纯JS总是更好,但我不知道这个解决方案如何解决不知道CSS文件何时加载完成的问题. (4认同)

Pet*_*son 20

这应该在IE中工作:

$("head").append("<link rel='stylesheet' type='text/css' href='/inc/body/jquery/css/start/jquery-ui-1.8.10.custom.css' />");
Run Code Online (Sandbox Code Playgroud)

  • 反过来也应该有效:`$('<link ... />').appendTo($('head'))`.这将允许你链接`link`元素,而不是`head`元素,如果你想在将它附加到`<head>`后用它做其他事情. (7认同)
  • 根据这个(http://stackoverflow.com/questions/5085228/does-jquery-append-behave-asynchronously),append不会异步行为. (7认同)

Hen*_*wan 10

正如RequireJS文档中提到的,棘手的部分在于加载CSS:

function loadCss(url) {
    var link = document.createElement("link");
    link.type = "text/css";
    link.rel = "stylesheet";
    link.href = url;
    document.getElementsByTagName("head")[0].appendChild(link);
}
Run Code Online (Sandbox Code Playgroud)

但是在知道何时/是否已成功加载CSS时,如在您的用例中"我希望在所有其他数据,图像,样式等加载加载jQuery UI ".

理想情况下,RequireJS可以加载CSS文件作为依赖项.但是,知道何时加载CSS文件存在问题,特别是在从另一个域加载文件时的Gecko/Firefox中.在这个Dojo门票中可以找到一些历史记录.

知道何时加载文件很重要,因为您可能只想在样式表加载后获取DOM元素的尺寸.

有些人已经实现了一种方法,他们寻找一种众所周知的样式来应用于特定的HTML元素,以了解是否加载了样式表.由于该解决方案的特殊性,它不适合RequireJS.知道链接元素何时加载了引用的文件将是最强大的解决方案.

由于知道文件何时加载不可靠,因此在RequireJS加载中明确支持CSS文件没有意义,因为它会因浏览器行为而导致错误报告.


归档时间:

查看次数:

35045 次

最近记录:

9 年,9 月 前