如何在 Webkit (Safari/Chrome) 中动态加载 css 规则?
我目前在 Webkit(Safari 和 Chrome)中遇到问题,我尝试动态加载(innerHTML)一些 html 到 div 中,该 html 包含 css 规则(...),在渲染 html 后,未加载样式定义(因此从视觉上我可以看出样式不存在,而且如果我用 javascript 搜索它们也找不到样式)。 我尝试过使用 jquery 插件 tocssRule(),它可以工作,但速度太慢了。 还有另一种方法让 webkit 动态加载样式吗? 谢谢。 帕特里克
I currently have issues in Webkit(Safari and Chrome) were I try to load dynamically (innerHTML) some html into a div, the html contains css rules (...), after the html gets rendered the style definitions are not loaded (so visually I can tell the styles are not there and also if I search with javascript for them no styles are found).
I have tried using a jquery plugin tocssRule(), it works but it is just too slow. Is there another way of getting webkit to load the styles dynamically?
Thanks.
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为最好的做法是将“链接”标签附加到文档的头部。 如果不可能,请尝试在头部附加一个“style”标签。 样式标签不应该出现在正文中(甚至不验证)。
附加链接标签:
附加样式标签:
I think it's a better practice to append a "link" tag to the head of your document. If that isn't possible, try to append a "style" tag to the head. Style tags shouldn't be in the body (Doesn't even validate).
Append link tag:
Append style tag:
直接的 jQuery 方式(适用于主要浏览器 - 包括 Chrome): http://topsecretproject.finitestatemachine.com/2009/09/how-to-load-javascript-and-css-dynamically-with-jquery/
从网站:
注意:示例中还包含了 JS 的注入,如果只想注入 CSS 引用则可以忽略。
Straight-up jQuery way (that works on major browsers - including Chrome): http://topsecretproject.finitestatemachine.com/2009/09/how-to-load-javascript-and-css-dynamically-with-jquery/
From the site:
Note: the example also includes injection of JS, which can be ignored if you just want to inject a CSS reference.
最简单的方法(即使用 jQuery)如下:
如果您这样做,那么它将在 Safari 中工作。 如果您以正常的 jQuery 方式(全部在一个文本字符串中)执行此操作,它将失败(CSS 将无法加载)。
然后你只需使用 $('head').append(cssLink); 将其附加到头部即可
simplest way (using jQuery that is) is the following:
If you do it that way then it will work in Safari. If you do it the normal jQuery way (all in one text string) it will fail (the css won't load).
Then you just append it to the head with $('head').append(cssLink);
谢谢Vatos,你给了我一个很好的引导,基本上我按照你的建议做了,但使用jquery设置html并将新样式附加到头部,因为safari/chrome在执行innerHTML并且document.head未定义时会停止。 我的代码看起来像这样
Thanks Vatos, you gave me a good lead, basically I did what you suggested but used jquery to set the html and append the new style to the head since, safari/chrome would stall when doing innerHTML and document.head was undefined. My code ended looking like this
嗯。 我没有足够的积分来投票支持 Andrei Cimpoca 的结果,但该解决方案是这里最好的解决方案。
style.innerHTML = "..."; 不适用于 webkit 引擎或 ie。
要正确注入 css 文本,您必须:
style.styleSheet.cssText = "..."; 对于即。
和
style.appendChild(document.createTextNode("...")); 对于网络工具包。
Firefox 也可以使用第二个选项,或者使用 style.innerHTML = "...";
meh. I don't have enough points to vote up Andrei Cimpoca's result, but that solution is the best one here.
style.innerHTML = "..."; does not work in webkit engines or ie.
To correctly inject css text, you must:
style.styleSheet.cssText = "..."; for ie.
and
style.appendChild(document.createTextNode("...")); for webkit.
Firefox will also work with the second option, or with style.innerHTML = "...";
这是演出。 帕特里克所说的对我不起作用。 我是这样做的。
InnerHTML 已被弃用,不应该用于这样的事情。 此外,它也更慢。
Here's the gig. What Patrick said did not work for me. Here's how I did it.
InnerHTML is deprecated and shouldn't be used for such a thing. Furthermore it is slower too.
除了上面基于样式标签的版本之外,您还可以直接使用 javascript 添加样式:
这具有能够使用变量而无需求助于 cssText/innerHTML 的优点。
请记住,在基于 webkit 的浏览器上,如果您插入大量规则,速度可能会很慢。 webkit 中存在一个用于修复此性能问题的 bug (https://bugs.webkit .org/show_bug.cgi?id=36303)。 在此之前,您可以使用样式标签进行批量插入。
这是插入样式规则的 W3C 标准方式,但任何版本的 IE 都不支持: http://www.quirksmode.org/dom/w3c_css.html
Besides the style tag based versions above, you can also add styles using javascript directly:
This has the advantage of being able to use variables without having to resort to cssText/innerHTML.
Keep in mind that on webkit based browsers this can be slow if you are inserting a lot of rules. There is a bug open to fix this performance issue in webkit (https://bugs.webkit.org/show_bug.cgi?id=36303). Until then, you can use style tags for bulk inserts.
This is the W3C standard way of inserting style rules, but it is not supported by any version of IE: http://www.quirksmode.org/dom/w3c_css.html
我知道这是一个较旧的话题,但认为有人会受益。 我也需要将 css 动态加载到我的页面中(在加载所有内容之前,根据包含的“组件”模板):
我尝试使用单行方法附加到 document.head - 在 IE 10 和 Chrome 中工作,但在 IE 10 和 Chrome 中工作,但在苹果浏览器。 上述方法在所有 3 种情况下均有效。
I know it's an older topic, but thought someone would benefit. I too needed to load css dynamically into my page (before everything rendered onload, according to included "component" templates):
I tried appending to document.head with a one-line approach -- worked in IE 10 and Chrome, but not in Safari. The approach above worked in all 3.