Created
June 24, 2011 09:13
-
-
Save jinwei233/1044471 to your computer and use it in GitHub Desktop.
artDialog note ——动态添加样式表
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
<!doctype html> | |
<html> | |
<head> | |
<title>demo</title> | |
<style type="text/css" media="screen"> | |
</style> | |
</head> | |
<body> | |
<div id="test"> | |
</div> | |
<script type="text/javascript"> | |
//缓存,避免重复添加style标签 | |
var _style = {}; | |
addHeadStyle = function(content, doc) { | |
doc = doc || document; | |
var style = _style[doc]; | |
if(!style){ | |
style = _style[doc] = doc.createElement('style'); | |
style.setAttribute('type', 'text/css'); | |
doc.getElementsByTagName("head")[0].appendChild(style); | |
}; | |
style.styleSheet && (style.styleSheet.cssText += content) || style.appendChild(doc.createTextNode(content)); | |
}; | |
if(1){ | |
addHeadStyle("#test{width:50px;height:50px;background:black;}"); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment