<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>抽獎</title>
</head>
<body>
<div class="logo">
<h1>
<img src="logo.png">
</h1>
</div>
<button type="button" onclick="myFunction()">點擊這裡</button>
<script>
function myFunction()
{
var randoms=[];
for(i=0;i<=100;i++)
{
var isExists=false;
var random=parseInt(Math.random()*(100-1))+1;
for (var i = 0; i < randoms.length; i++)
{
if (random!=randoms[i])
{
isExists = true;
break;
}
}
if (!isExists)
randoms.push(random);
if (randoms.length === 1)
break;
}
window.alert(randoms);
}
</script>
</body>
</html>
這個比較簡單了,給你段js代碼,你在頁面添加一個按鈕執行 這個 getLuckyDraw() js函數就行
var gIndexArray = new Array(100);
window.onload=function()
{
alert("load");
for(var i=0;i<100;++i)
{
gIndexArray[i] = i+1;
}
};
function getLuckyDraw()
{
if(gIndexArray.length>0)
{
var index = Math.round(Math.random()*gIndexArray.length);
if(index==gIndexArray.length)
{
index = index-1;
}
alert(gIndexArray[index]);
gIndexArray.splice(index,1);
}
else
{
alert("Draw the end !");
}
}