如何用javascript创建一个style元素。

招RD和QA 2006-11-27 11:02:46
<style type = "text/css">

.wrs_link
{
font-weight:bold; text-decoration:underline; cursor:hand;
}

</style>

我使用document.createElement();来创建,发现总是一个空的元素?请问怎样用javascript动态创建这样一个东西?
...全文
144 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dh20156 2006-11-28
  • 打赏
  • 举报
回复
偶的dhATV里的一段,参考:
/** Mozilla兼容MsIE脚本,stylesheet扩展部分。
* o stylesheet.addRule()
*/
(function () {
if (! window.CSSStyleSheet) return;
function _ss_GET_rules_ () {
return this.cssRules;
}
var _ss = CSSStyleSheet.prototype;
_ss.addRule = function(sSelector, sRule) {
this.insertRule(sSelector + "{" + sRule + "}", this.cssRules.length);
}
_ss.__defineGetter__("rules", _ss_GET_rules_);
})();
//对象实例化
this.setup = function(obj){
if(typeof document.styleSheets=="undefined" || document.styleSheets!="OBJECT"){
var styleObj = document.createElement("STYLE");
document.getElementsByTagName("HEAD")[0].appendChild(styleObj);
}
var oStyleSheet = document.styleSheets[0];
oStyleSheet.addRule(".root","margin:0px;");
oStyleSheet.addRule(".root *","font-size:"+this.textsize+";");
oStyleSheet.addRule(".root div","margin-left:17px;white-space: nowrap;");
oStyleSheet.addRule(".childnode","background:url("+this.icoFolder+"/i.gif) left top repeat-y;}");
oStyleSheet.addRule(".childvalue","background:url("+this.icoFolder+"/i.gif) left top repeat-y;");
oStyleSheet.addRule(".nodetext","cursor:pointer;");
oStyleSheet.addRule(".nodepanel","display:none;");
var rootStr = "<div class=\"root\" fid=\"0\"><img src=\""+this.icoFolder+"/dashplus.gif\" align=\"absmiddle\" onclick=\""+this.treeTagName+".clickNode(this.parentNode);\" /><img src=\""+this.icoFolder+"/ro.gif\" align=\"absmiddle\" /> <span class=\"nodetext\" onclick=\""+this.treeTagName+".clickNode(this.parentNode);\">"+this.rootText+"</span><span class=\"nodepanel\" /></div>"
obj.innerHTML = rootStr;
}
煊烨 2006-11-28
  • 打赏
  • 举报
回复
var ostyle= document.createStyleSheet();
ostyle.addRule("DIV B", "color:blue", 0);

87,909

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧