简体   繁体   English

在页面上加载样式表(不仅是DOM)时触发的事件

[英]Event which triggers when stylesheets, not only the DOM, are loaded on the page

I tried to use the http://api.jquery.com/ready/ callback for executing a code, which I would like to get started only when the page is fully loaded. 我尝试使用http://api.jquery.com/ready/回调执行代码,只有在页面完全加载后,我才想开始使用该代码。 However, this triggers when the DOM is loaded, and it can often happen that the stylesheets are not loaded fully. 但是,这会在加载DOM时触发,并且经常发生样式表未完全加载的情况。

How can I ensure that a particular code is loaded after DOM and also the stylesheets (or even everything) are loaded? 如何确保在DOM之后加载特定的代码以及样式表(甚至所有内容)是否加载? I found some similar questions (like this one ), but they are not about stylesheet loading, which I am particularly interested in. 我发现了一些类似的问题(例如这个问题),但是它们与样式表加载无关,我对此特别感兴趣。

Thanks for suggestions. 感谢您的建议。

If you really don't mind waiting for everything (including images, etc) to load, then you can use the standard load event: 如果您真的不介意等待所有内容(包括图像等)加载,则可以使用标准的load事件:

$(window).load(function() {
    // your code here
});

I first thought of RequireJS but it appears they have specific reasons why it wouldn't work well. 我首先想到RequireJS,但似乎他们有特定的原因导致它无法正常运行。 http://requirejs.org/docs/faq-advanced.html#css http://requirejs.org/docs/faq-advanced.html#css

They do offer a possible hack "Some people have implemented an approach where they look for a well known style to be applied to a specific HTML element to know if a style sheet is loaded." 他们确实提供了一个可能的技巧:“有些人已经实现了一种方法,即他们将一种众所周知的样式应用于特定的HTML元素,以了解是否加载了样式表。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM