我有一个数组。
var array = [0,1,2,4];
var index;
现在我有四个按钮。我可以按随机顺序单击任何按钮,我需要将索引值更新为 0(代表单击的第一个按钮)、1(代表第二个按钮)、2(代表第四个按钮)、3(代表第三个按钮)。
最佳答案
HTML:
<input type="button" onclick="javascript:SetIndex(this)" value="one" />
<input type="button" onclick="javascript:SetIndex(this)" value="two" />
<input type="button" onclick="javascript:SetIndex(this)" value="three" />
<input type="button" onclick="javascript:SetIndex(this)" value="four" />
JavaScript:
var array = [0,1,2,4];
var index = 0;
function SetIndex(obj)
{
// check if index is out of range and it might be useful
// to see if this button already has an index assigned
if (index < array.length && isNaN(obj.index))
{
obj.title = array[index]; // hover to see index...
obj.index = array[index];
index++;
}
}
<小时/>
关于javascript - 从数组中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5732804/