链接元素onload [英] link element onload

查看:86
本文介绍了链接元素onload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有听到onload事件一个< link> 元素?

Is there anyway to listen to the onload event for a <link> element?

F。例如:

var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'styles.css';

link.onload = link.onreadystatechange = function(e) {
    console.log(e);
};

这适用于< script> 元素,但不是< link> 。还有另一种方式吗?
我只需要知道外部样式表中的样式何时应用于DOM。

This works for <script> elements, but not <link>. Is there another way? I just need to know when the styles in the external stylesheet has applied to the DOM.

更新:

将注入隐藏的< iframe> 是一个想法,添加< link> 到头,并在iframe中收听 window.onload 事件?它应该在css加载时触发,但它可能不能保证它被加载到顶层窗口中。

Would it be an idea to inject a hidden <iframe>, add the <link> to the head and listen for the window.onload event in the iframe? It should trigger when the css is loaded, but it might not guarantee that it's loaded in the top window...

推荐答案

这是一个黑客,但如果你可以编辑CSS,你可以添加一个特殊的样式(没有可见的效果),你可以在这篇文章中使用这个技巧来听: http://www.west-wind.com/weblog/posts/478985.aspx

This is kind of a hack, but if you can edit the CSS, you could add a special style (with no visible effect) that you can listen for using the technique in this post: http://www.west-wind.com/weblog/posts/478985.aspx

您将需要页面中包含CSS或CSS的元素。当您的代码检测到其样式已更改时,CSS已加载。

You would need an element in the page that has a class or an id that the CSS will affect. When your code detects that its style has changed, the CSS has been loaded.

一个黑客,正如我所说:)

A hack, as I said :)

这篇关于链接元素onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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