jquery - Up and down keys to loop through array -
I have an array of colors, using the up and down arrow keys, I would like to loop through the array and the current I will add the ARA value as the square of a div.
var color = ["red", "green", "blue"];
I would like to use jQuery .addClass to store the current value as a variable and add an existing class in a div. As a jQuery newcomer, any help here would be great!
1 reminder %
var color = ["red", "green", "blue"], i = 0, n = colors.length; $ (Document) .Kiddown (function (e) {var k = ew; if (k == 38 || k == 40) {i = (k == 38? ++ i: - i) and lieutenant ; 0? N-1: I% n; $ ('# test'). At ('class', color [i]);}}); Controlling the internal array with another interesting ( less complex ) method without using current index count
Always [0] get the index key
always by pushing the last key in position (or inverted, pre-final) :
var color = ["red", "green", "blue"]; $ (Document) .Kiddown (function (e) {var k = ew; if (k == 38 || k == 40) {if (k == 38) color.push (colors.shift ()) ; And if (k == 40) color. Anshan (colors.pop ()); $ ('# test'). At ('square', color [0]);}});
Comments
Post a Comment