ITeye博客

JavaScript动态添加CSS

cjx186 · 2011-12-19 00:28 · js、nodejs · Web前端 · Markdown

function addCssByStyle(cssString){
	var doc=document;
	var style=doc.createElement("style");
	style.setAttribute("type", "text/css");

	if(style.styleSheet){// IE
		style.styleSheet.cssText = cssString;
	} else {// w3c
		var cssText = doc.createTextNode(cssString);
		style.appendChild(cssText);
	}

	var heads = doc.getElementsByTagName("head");
	if(heads.length)
		heads[0].appendChild(style);
	else
		doc.documentElement.appendChild(style);
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。