javascript - Add differing CSS rules to a set of generated elements -
javascript - Add differing CSS rules to a set of generated elements -
i want loop , add together several multicolored spans div container. efficiently, alter css
, instead of adding new color class element:
var coloren = ["royalblue", "lawngreen", "red", "orange", "yellow", "black", "white", "mediumorchid"]; ( = 0; < coloren.length; i++ ) { var $span = $('<span />').attr('class', 'coloratorsquare'); $span.css({background : coloren[i]}); $("#colorator").append($span); }
generating, example:
<span class="coloratorsquare active" style="background: rgb(65, 105, 225);"></span>
then, when select (click) span, alter color silver show has been selected, , set other sibling spans original colors. here's snippet:
$(this).addclass("active").siblings().removeclass("active");
the problem is, if alter css
of span elements ($span.css(...)
), doesn't apply css
changes on add/remove class. if comment out changing css of span, multiple colors aren't added, active
class add/removal of selected/deselected span changes colors expected:
// $span.css({background : coloren[i]});
css:
.active { background-color: silver; color: black; }
i not alter .css
of span @ all, don't think makes sense add together classes each span during generation, , have add together css
class rules each color replicate .css
functionality.
my question: how can add together multiple different css rules (ex: multicolors) randomly generated elements without a) having generate rules manually in css file, , b) altering .css
jquery such causes problems adding/removing css class rules.
sorry if unclear.
thanks!
you could utilize !important
on silver color have override locally set attributes of elements, technique frowned upon.
my recommendation consider circumstances; if not throwaway code, makes improve sense set classes on elements instead of straight manipulating style attribute. if it's need mockup means slap !important
on , move on.
javascript jquery html css css3
Comments
Post a Comment