在我的程序中,我想用jquery / javascript兼做两件事:
首先,我使用$(".className").css()
method,但是它仅更改那些已经具有className
类的元素的样式,即,如果我以后将className
其添加到元素中,其样式将不会是新的。我该如何解决?
在jsfiddle上也可以看到它。
$("p").addClass("redclass");
$(".redclass").css("color", "darkRed");
$("span").addClass("redclass");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>I want to be red! And I am.</p>
<span>I want to be red too but I'm not :'(</span>
结果:
更为简短的格式:
$("<style/>", {text: ".redclass {color: darkRed;}"}).appendTo('head');
片段:
$("<style/>", {text: ".redclass {color: darkRed;}"}).appendTo('head');
$("p").addClass("redclass");
$("span").addClass("redclass");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>I want to be red! And I am.</p>
<span>I want to be red too but I'm not :'(</span>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句