js动态加载css文本

<html>
	<head>
		<title></title>
		<script language="javascript">

function insertStyle(cssText) {
	var head = document.getElementsByTagName("head")[0];
	var style = document.createElement("style");
	var rules = document.createTextNode(cssText);
	style.type = "text/css";
	if (style.styleSheet) {
		style.styleSheet.cssText = rules.nodeValue;
	} else {
		style.appendChild(rules);
	}
	head.appendChild(style);
}
var cssText = "h1 { background: red; }";
insertStyle(cssText);


</script>
	</head>
	<body>
<h1>hello</h1>
	</body>
</html>
 

你可能感兴趣的:(css)