jQuery.ready()函数和异步CSS [英] jQuery.ready() function and asynchronous CSS

查看:58
本文介绍了jQuery.ready()函数和异步CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想异步加载我的网站CSS。我想知道是否 jQuery.ready()函数将在完成CSS的异步加载后运行,或者jQuery会忽略它并且CSS可能在<$ c之后完成加载$ c> jQuery.ready()函数开始运行。

I want to load my website CSS asynchronously. I want to know if jQuery.ready() function will run after the async load of the CSS is completed or will jQuery ignore it and the CSS might finish loading after jQuery.ready() function starts running.

推荐答案

尝试使用 $。holdReady()

// set `$.holdReady()` to `true`
$.holdReady(true);
// do stuff at `$.holdReady(true)`
// e.g., call hide `body` element
$("body").hide();
// load asynchronous `css`
$("<style>")
.appendTo("head")
.load("https://gist.githubusercontent.com/anonymous/ec1ebbf8024850b7d400/"
      + "raw/906431c0472286e141b91aac1676369bbb8d2f97/style.css"
      , function(e) {
  // set `$.holdReady()` to `false`
  $.holdReady(false);
});
// do stuff at `$.holdReady(false)`
$(document).ready(function() {
  $("body").show();
  alert("ready");
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<body>
  <div>
    abc
  </div>
    <div>
    123
  </div>
</body>

这篇关于jQuery.ready()函数和异步CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆