[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>随机获取两个名字</title>
</head>
<body>
<div id="div1"></div>
</body>
<script>
function getRandomArrayElements(arr, count) {
var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
}
var items = ['李白','杜甫','孟浩然','苏轼'];
document.getElementById('div1').innerHTML = getRandomArrayElements(items, 2);
</script>
</html> |