【发布时间】:2015-03-27 08:45:08
【问题描述】:
嘿,我的阵列需要一些帮助。我可以通过document.getElementById 看到它捕获了我的值,但我希望该值成为 div 中的背景样式。我知道它看起来很混乱 <li> 和 <div> 里面但我真的迷路了,我无法让它工作。
我需要为数组中的每个项目创建一个元素,并将它们的值作为背景颜色。
如果我的问题没有意义,我很抱歉,因为太晚了;-)
var noteColors = [
{ name: "red", value: "#FF396D" },
{ name: "green", value: "#C6DD2A" },
{ name: "blue", value: "#55C7F0" },
{ name: "purple", value: "#F64FC2" },
{ name: "yellow", value: "#F6DC2C" }];
<div id="note_colors_wrapper">
<script type="text/javascript">
var index;
var text = "<ul id='colorss'>";
for (index = 0; index < noteColors.length; index++) {
text += "<li class='ol'>" + noteColors[index].value + "</li>";
}
text += "</ul>";
document.getElementById("note_colors_wrapper").innerHTML = text;
$(".ol").append("<div id='lo' style=background-color:" +
noteColors[index].value + ";></div>");
</script>
</div>
最终结果应该是这样的
【问题讨论】:
-
你能举个例子说明你想要的输出是什么样的吗?
-
当
append()(带有背景色的div)运行时,index将超过noteColors数组的末尾。 -
@DanPrince 我添加了一个 img :-)
-
@PaulRoub 我该如何解决? :-)
标签: javascript jquery html css arrays