gpt4 book ai didi

javascript - CSS 交付优化 : How to defer css loading?

转载 作者:IT王子 更新时间:2023-10-29 02:55:45 25 4
gpt4 key购买 nike

我正在尝试按照开发人员的谷歌文档优化 CSS 交付 https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery#example

正如您在内联小型 CSS 文件的示例中看到的那样,关键 CSS 内联在头部,原始 small.css 在页面加载后加载

<html>
<head>
<style>
.blue{color:blue;}
</style>
</head>
<body>
<div class="blue">
Hello, world!
</div>
</body>
</html>
<noscript><link rel="stylesheet" href="small.css"></noscript>

关于这个例子我的问题:

如何在页面加载后加载一个大的css文件?

最佳答案

如果您不介意使用 jQuery,这里有一个简单的代码片段可以帮助您。 (否则评论,我会写一个纯js的例子

function loadStyleSheet(src) {
if (document.createStyleSheet){
document.createStyleSheet(src);
}
else {
$("head").append($("<link rel='stylesheet' href='"+src+" />"));
}
};

只需在您的 $(document).ready()window.onload 函数中调用它即可。

对于 #2,您为什么不尝试一下呢?在您的浏览器中禁用 Javascript 并查看!

顺便说一句,一个简单的 google 搜索可以让您走多远,这真是令人惊讶;对于查询 "post load css",这是第四次命中... http://www.vidalquevedo.com/how-to-load-css-stylesheets-dynamically-with-jquery

关于javascript - CSS 交付优化 : How to defer css loading?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com