Skip to content

Instantly share code, notes, and snippets.

@jinwei233
Created June 24, 2011 09:13
Show Gist options
  • Save jinwei233/1044471 to your computer and use it in GitHub Desktop.
Save jinwei233/1044471 to your computer and use it in GitHub Desktop.
artDialog note ——动态添加样式表
<!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