<html>
<head>
<meta charset="utf-8"/>
<meta name="format-detection" content="telephone=no">
<title>test</title>
<style type="text/css">
body{text-align: center}
.div1{
max-width: 1000px
margin: 50px auto
padding: 20px 0
background-color: #efa
}
</style>
</head>
<body>
<div class="div1">试试手气</div>
<button class="btn">点击抽奖</button>
<script type="text/javascript">
window.onload=function(){
// var form=document.forms[0]
var oDiv=document.querySelector(".div1")
var oBtn=document.querySelector(".btn")
var arr=[
{item:"美女一个",chances:20},
{item:"巴掌一个",chances:15},
{item:"嗜血珠",chances:20},
{item:"摄魂棒",chances:20},
{item:"诛仙剑",chances:25}
] //奖项对象
// 处理数据
var start_num=0
for(var i=0,len=arr.lengthi<leni++){
if(i!=0){
start_num+=arr[i-1]["chances"]
}
arr[i].range=[start_num,(start_num+arr[i]["chances"]-1)]
}
oBtn.onclick=function(){ //绑定事件
setFunc()
}
console.log(arr)
function setFunc(){
var num=parseInt(Math.random()*100)
for(var j=0,len=arr.lengthj<lenj++){
if(arr[j]["range"][0]<=num && arr[j]["range"][1]>=num){
oDiv.innerHTML="恭喜你获得"+arr[j]["item"]
}
}
}
}
</script>
</body>
</html>
写了个例子,可以参考一下思路
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>jquery版的网页倒计时效果</title>
<style type="text/css">
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
var intDiff = parseInt(86400) //倒计时总秒数量(1天=24*60*60=86400)
function timer(intDiff) {
window.setInterval(function() {
var day = 0,
hour = 0,
minute = 0,
second = 0 //时间默认值
if(intDiff > 0) {
day = Math.floor(intDiff / (60 * 60 * 24))
hour = Math.floor(intDiff / (60 * 60)) - (day * 24)
minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60)
second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
}
if(minute <= 9) minute = '0' + minute
if(second <= 9) second = '0' + second
$('#day_show').html(day + "天")
$('#hour_show').html('<s id="h"></s>' + hour + '时')
$('#minute_show').html('<s></s>' + minute + '分')
$('#second_show').html('<s></s>' + second + '秒')
intDiff--
}, 1000)
}
$(function() {
timer(intDiff)
})
</script>
</head>
<body>
<div class="time-item">
<span id="day_show">0天</span>
<span id="hour_show">0时</span>
<span id="minute_show">0分</span>
<span id="second_show">0秒</span>
</div>
</body>
</html>
form
http://www.jq22.com/download/pd-3d2e942a-2a02-11e4-ac58-000c29b03321.zip