<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
</style>
</head>
<body>
<input type="button" value="随机选择一个" id="btn">
<br>
<select id="food">
<option>烤肉</option>
<option>拉面</option>
<option>麻辣烫</option>
<option>小龙虾</option>
<option>火锅</option>
<option>外卖</option>
</select>
<script>
//获取元素
var btn = document.getElementById("btn");
var food = document.getElementById("food");
var opts = food.getElementsByTagName("option");
//按钮添加点击事件
btn.onclick = function(){
//随机选择一个option
//每次点击需要获一个opts数组的随机下标
//Math.random() [0,1]
//Math.random()*6 [0,6]
var n = Math.floor(Math.random() * opts.length);
//console.log(n);
opts[n].selected = true;
};
</script>
</body>
</html>