js代码
<script type="text/javascript"> window.onload = function(){ var btn = document.getElementById('btn'); var cm = document.getElementById('cm'); // 获取html中的菜单 var menu = document.getElementsByTagName('span'); var menuList = []; // 遍历菜单逐个加到新菜单menuList数组中 for(var i=0;i<menu.length;i++){ menuList.push(menu[i].innerHTML); } btn.onclick = function(){ var num = Math.floor(Math.random()*(menuList.length-1+1-0)+0); cm.innerHTML = menuList[num]; } } </script>