$(document).ready(function() {
var colorArray = new Array(
"#ff0000",
"#000000",
"#00ff00",
"#0000ff"
);
var randColor, randListElem;
var listElems = $('li');
for(var i = 0; i < 3; i++)
{
//randColor = Math.floor(Math.random()*(colorArray.length));
colorArray.sort(function() { return 0.5 - Math.random();});
var ran = colorArray.pop();
randListElem = Math.floor(Math.random()*(listElems.length));
$(listElems[randListElem]).css("background", colorArray[ran]);
};
});
Once I run the last command in the loop I want to remove the color in colorArray from being used again. How can I do this?
Since you're using jQuery
colorArray.splice($.inArray(randColor, myarray), 1);
If the item isn't in the array. Something a little better
var index = $.inArray(randColor, colorArray);
if (index>=0) colorArray.splice(index, 1);
Or
var itemtoRemove = randcolor;
arr.splice($.inArray(itemtoRemove, colorArray),1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.