Skip to content

Instantly share code, notes, and snippets.

@Justineo
Last active November 24, 2016 09:05
Show Gist options
  • Save Justineo/6667972 to your computer and use it in GitHub Desktop.
Save Justineo/6667972 to your computer and use it in GitHub Desktop.
Correct way to create stylesheets dynamically
function createStyle(styleText) {
var style = document.createElement('style');
style.type = 'text/css';
// <style> element must be appended into DOM before setting `cssText`
// otherwise IE8 will interpret the text in IE7 mode.
document.body.appendChild(style);
if (style.styleSheet) {
style.styleSheet.cssText = styleText;
} else {
style.appendChild(document.createTextNode(styleText));
}
}
@yisibl
Copy link

yisibl commented Jun 16, 2014

一些模版会经常会用到兼容IE6、7 的 hack:

.foo {
  display: inline-block;
  *display: inline;
  *zoom: 1;
}

通过动态插入后,display: inline也会被识别,覆盖掉上面的display: inline-block

感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment