这实际上是我所做的。
为了确保加载特定的 CSS 文件,我在 CSS 文件的末尾添加了一个样式。例如:
#ensure-cssload-8473649 {
display: none
}
现在我有一个 JavaScript 函数,它会在页面上加载上述样式时触发指定的回调:
var onCssLoad = function (options, callback) {
var body = $("body");
var div = document.createElement(constants.TAG_DIV);
for (var key in options) {
if (options.hasOwnProperty(key)) {
if (key.toLowerCase() === "css") {
continue;
}
div[key] = options[key];
}
}
var css = options.css;
if (css) {
body.appendChild(div);
var handle = -1;
handle = window.setInterval(function () {
var match = true;
for (var key in css) {
if (css.hasOwnProperty(key)) {
match = match && utils.getStyle(div, key) === css[key];
}
}
if (match === true) {
window.clearTimeout(handle);
body.removeChild(div);
callback();
}
}, 100);
}
}
这就是我如何使用上面的函数:
onCssLoad({
"id": "ensure-cssload-8473649",
css: {
display: "none"
}
}, function () {
// code when you want to execute
// after your CSS file is loaded
});
这里的第一个参数采用options
where id
is 检查测试样式和 css 属性,以验证从 CSS 加载的内容。