【问题标题】:Append CSS to head?将 CSS 附加到头部?
【发布时间】:2012-11-25 18:20:06
【问题描述】:

我想在 head 标签中添加一些 css 类,使用 jQuery(而不是像 Jquery add css to head 这样的 css 文件)

我的css类是这样的:

#control1 { width:60px; height:55px; background:url(../images/logo1.png) no-repeat; float:left; }
#control1.selected {background:url(../images/logo1_aktiv.png) no-repeat;}

感谢您的帮助。

【问题讨论】:

  • 同样的事情,只需在接受的答案中将link 元素替换为style 元素..
  • 您想在样式表中添加规则吗?
  • 你为什么要这样做?为什么不内联添加样式?为什么不事先定义样式,然后用 JS 切换?

标签: javascript jquery html css class


【解决方案1】:

这是你需要做的,将你的样式附加到head标签

var yourClasses="#control1 { width:60px; height:55px; background:url(../images/logo1.png) no-repeat; float:left; }
#control1.selected {background:url(../images/logo1_aktiv.png) no-repeat;}";

$("document").ready(function(){
    $("head").append("<style type='text/css'>"+yourClasses+"</style>");
});

【讨论】:

  • 非常感谢,我已经对其进行了编辑并为每个 css 类创建了一个变量 :)
【解决方案2】:

您可以使用appendTo() 将其附加到头部,然后选择"head"

$("<style type='text/css'> .boldYellow{color:yellow; font-weight:bold;} </style>").appendTo("head");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2013-05-27
    相关资源
    最近更新 更多