Last active
November 24, 2016 09:05
-
-
Save Justineo/6667972 to your computer and use it in GitHub Desktop.
Correct way to create stylesheets dynamically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一些模版会经常会用到兼容IE6、7 的 hack:
通过动态插入后,
display: inline
也会被识别,覆盖掉上面的display: inline-block
。感谢!