onclick我希望它将按钮的值更改为数组中的随机字符串

时间:2014-11-01 15:51:13

标签: javascript

function randomize(){
    var maps =["Aztec","Cache","Cobblestone","Dust","DustII","Inferno",
    "Mirage","Nuke","Overpass","Train","Vertigo","Assault","Italy","Militia","Office,"];
    var randommap = maps[math.floor(math.random() * maps.lenth)];
    var randommap = document.getElementById("map").innerHTML = randommap;
    }

1 个答案:

答案 0 :(得分:1)

这是一个非常简洁的问题。同样如此,这是一个简洁但构造良好的答案:



var maps = ['Aztec', 'Cache', 'Cobblestone', 'Dust',
  'DustII', 'Inferno', 'Mirage', 'Nuke', 'Overpass',
  'Train', 'Vertigo', 'Assault', 'Italy', 'Militia',
  'Office'];

function randomItem(list) {
  var randomIndex = Math.floor(Math.random() * list.length);
  return list[randomIndex];
}

var mapEl = document.getElementById('map');

mapEl.onclick = function() {
  mapEl.innerHTML = randomItem(maps);
};

<button id="map">Go!</button>
&#13;
&#13;
&#13;