2 回答
TA贡献1725条经验 获得超7个赞
试试下面的这个函数,你的代码中有很多错误,比如:
未定义的 var .
average_min
element
不能直接乘法,需要先得到元素的高度。此外,在分配高度时,您必须在修复后添加“px”。
以及这种说法有什么用。
htmlStr_min_chart = "";
function chart(){
var a1 = document.getElementById("111");
var a2 = document.getElementById("112");
var a3 = document.getElementById("113");
var a4 = document.getElementById("114");
var a5 = document.getElementById("115");
var a6 = document.getElementById("116");
var a7 = document.getElementById("117");
var a8 = document.getElementById("118");
var a9 = document.getElementById("119");
var a10 = document.getElementById("120");
var a11 = document.getElementById("121");
var a12 = document.getElementById("122");
a1.style.backgroundColor = "blue";
a2.style.height = "500px";
var divs = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12];
var average_min = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; // This is just an example of how you can use it.
htmlStr_min_chart = "";
for (let index = 0; index < average_min.length; index++) {
element = average_min[index];
old_height = (parseFloat(element) ? element : 1); // You use multiplication that's why I only use 1, otherwise if you use zero, any value will be zero.
divs[index].style.height = (old_height * 10) + "px"; // you have to set "px" to assign height.
divs[index].style.backgroundColor = "blue";
}
}
chart();
<style type="text/css">div{ margin-bottom: 5px; } </style>
<div id="111"></div>
<div id="112"></div>
<div id="113"></div>
<div id="114"></div>
<div id="115"></div>
<div id="116"></div>
<div id="117"></div>
<div id="118"></div>
<div id="119"></div>
<div id="120"></div>
<div id="121"></div>
<div id="122"></div>
添加回答
举报