问题描述
您好我这里有一个帖子了解释了什么是我想要达到的。
Hi I have a post here http://jsfiddle.net/7ztEf/9/ that explains what i want to achieve.
我会打破它,我需要填写基于数组索引值一个div BG,这些都是4的随机数1-40之间,说返回的数字是5,15,25,35。其中的逻辑是,如果数字范围1-10然后DIV BG是红色的,如果再11-20蓝色等
I will break it down, I need to fill a div bg based on the values of an array index, these are 4 random numbers between 1-40, say the numbers returned are 5,15,25,35. The logic is if numbers range 1-10 then div bg is red, if 11-20 then blue, etc
我的Ive在这个被杀死,真的需要一些建议再这样做的最好的办法,非常感谢P
Ive been killing myself over this, really need some advise re best way of doing this, many thanks P
function myNumbers(numbers, type) {
for (var x in numbers) {
document.getElementById(type + x).innerHTML = numbers[x];
/*
here I need to query each index value and apply to ball div, ie:
if index 0 numbers > 0) && numbers <= 10 than ball-0 div bg is red
if index 0 numbers > 11) && numbers <= 20 than ball-0 div bg is blue
if index 0 numbers > 21) && numbers <= 30 than ball-0 div bg is orange
if index 0 numbers > 31) && numbers <= 40 than ball-0 div bg is green
repeating for eack index value.
if index 1 numbers > 0) && numbers <= 10 than ball-1 div bg is red
etc
*/
}
}
通过myNumbers([5,15,25,35],测试 - ')
myNumbers([5, 15, 25, 35], 'test-')
推荐答案
有关短短的颜色,我只想写出来是这样的:
For just a few colors I would just write it out like this:
for (var x = 0, n = numbers.length; x != n; ++x) {
var nr = numbers[x],
el = document.getElementById(type + x),
color;
el.innerHTML = nr;
if (nr > 0) {
if (nr <= 10) {
color = 'red';
} else if (nr <= 20) {
color = 'blue';
} else if (nr <= 30) {
color = 'orange';
} else if (nr <= 40) {
color = 'green';
}
}
if (color) {
el.style.color = color;
}
}
这篇关于数组索引N.O. +价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!