最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實(shí)現(xiàn)抽獎(jiǎng)的兩種方法

 更新時(shí)間:2020年03月19日 12:00:23   作者:浪子一秋  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)抽獎(jiǎng)的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)抽獎(jiǎng)的具體代碼,供大家參考,具體內(nèi)容如下

抽獎(jiǎng)活動(dòng)的原理還是很簡單的,通過代碼一目了然,如果看不懂就私聊我,可以私下交流!

方法一:使用table寫一個(gè)隨機(jī)抽獎(jiǎng)

這是html+js代碼

<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <link rel="stylesheet" href="css/test2.css" >
 <title>抽獎(jiǎng)</title>
</head>
 
<body>
 <div class="content">
  <div class="top">
   抽獎(jiǎng)活動(dòng)
  </div>
  <div class="body">
   <table>
    <tr>
     <th>百度</th>
     <th>騰訊</th>
     <th>阿里</th>
     <th>京東</th>
     <th>華為</th>
    </tr>
    <tr>
     <th>滴滴</th>
     <th>螞蟻金服</th>
     <th>樂視</th>
     <th>中國電網(wǎng)</th>
     <th>中石化</th>
    </tr>
    <tr>
     <th>美團(tuán)</th>
     <th>樂視</th>
     <th>小米</th>
     <th>網(wǎng)易</th>
     <th>酷我</th>
    </tr>
    <tr>
     <th>愛奇藝</th>
     <th>盛大</th>
     <th>短文學(xué)</th>
     <th>淺墨詩韻</th>
     <th>浪子一秋</th>
    </tr>
   </table>
  </div>
  <div class="bottom">
   <input type="button" name="" id="button" class="button" value="開始" onclick="toStart(this)">
  </div>
 </div>
</body>
<script type="text/javascript">
 var timer;
 var button = document.querySelector("#button");
 function toStart() {
  // 啟動(dòng)定時(shí)器
  if (timer == undefined) {
   timer = setInterval(changeStyle, 100);
   button.setAttribute("value", "停止");
  } else {
   clearInterval(timer);
   timer = undefined;
   button.setAttribute("value", "開始");
  }
 }
 // 改變樣式
 var a, b;
 function changeStyle() {
  var tb = document.querySelector("table");
  console.log(a);
  if (a != undefined) {
   tb.rows[a].cells[b].style.backgroundColor = "white";
  }
  // 
  // 獲取要操作的元素
  a = parseInt(Math.random() * 4);
  b = parseInt(Math.random() * 5);
  // console.log(a);
  var col = tb.rows[a].cells[b];
  col.style.backgroundColor = "red";
 }
 
 
</script>
 
</html>

方法二:使用span標(biāo)簽寫

html+js代碼如下

<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <link rel="stylesheet" href="css/test2.css" >
 <title>抽獎(jiǎng)</title>
</head>
 
<body>
 <div class="content">
  <div class="top">
   抽獎(jiǎng)活動(dòng)
  </div>
  <div class="body" id="body">
 
  </div>
  <div class="bottom">
   <input type="button" name="" id="button" class="button" value="開始" onclick="toStart()">
  </div>
 </div>
</body>
<script type="text/javascript">
 // 獲取要操作的元素
 var div = document.getElementById("body");
 // 動(dòng)態(tài)添加span
 for (var i = 0; i < 25; i++) {
  // 創(chuàng)建一個(gè)新的標(biāo)簽
  var el = document.createElement("span");
  // 給標(biāo)簽設(shè)置內(nèi)容
  el.innerText = i;
  // 添加子元素
  div.appendChild(el);
 }
 
 var timer;
 var button = document.querySelector("#button");
 function toStart() {
  // 啟動(dòng)定時(shí)器
  if (timer == undefined) {
   timer = setInterval(changeStyle, 100);
   button.setAttribute("value", "停止");
  } else {
   clearInterval(timer);
   timer = undefined;
   button.setAttribute("value", "開始");
  }
 }
 // 改變樣式
 var selection;
 function changeStyle() {
  var spans = document.getElementsByTagName("span");
  if (selection != undefined) {
   console.log(selection);
   spans[selection].style.backgroundColor = "white";
  }
  selection = parseInt(Math.random() * 25);
  var spans = document.getElementsByTagName("span");
  var selectSpan = spans[selection];
  selectSpan.style.backgroundColor = "red";
 }
 
</script>
 
</html>

兩個(gè)頁面的css代碼

*{
 margin: 0;
 padding: 0;
}
body{
 display: block;
}
.content{
 width: 500px;
 margin: auto;
}
.top{
 text-align: center;
 height: 50px;
 color: red;
 font-size: 30px;
 
}
table{
 width: 100%;
 border: 1px solid red;
 border-spacing: 0;
}
th{
 border: 1px dashed rgb(189, 189, 86);
 height: 40px;
}
.bottom{
 height: 50px;
 margin-top: 20px;
 text-align: center;
}
.button{
 background-color: #4CAF50; /* Green */
 border: none;
 color: white;
 padding: 15px 32px;
 text-align: center;
 text-decoration: none;
 display: inline-block;
 font-size: 16px;
}
 
 
/* test2-1 */
.body{
 width: 512px;
 height: 260px;
 border: 1px solid red;
}
span{
 display: inline-block;
 width: 100px;
 height: 50px;
 border: 1px dashed #b1da1f;
 line-height: 50px;
 text-align: center;
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • swiper實(shí)現(xiàn)異形輪播效果

    swiper實(shí)現(xiàn)異形輪播效果

    這篇文章主要為大家詳細(xì)介紹了swiper實(shí)現(xiàn)異形輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • JS輪播圖的實(shí)現(xiàn)方法2

    JS輪播圖的實(shí)現(xiàn)方法2

    這篇文章主要為大家詳細(xì)介紹了JS輪播圖的具體實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • js parseInt的陷阱分析小結(jié)

    js parseInt的陷阱分析小結(jié)

    js parseInt的陷阱分析小結(jié),當(dāng)?shù)谝粋€(gè)字符為0時(shí),Js會(huì)把它看成一個(gè)8進(jìn)制數(shù)字,其他8進(jìn)制之外的字符都回被忽略掉。
    2011-03-03
  • JavaScript 完成注冊(cè)頁面表單校驗(yàn)的實(shí)例

    JavaScript 完成注冊(cè)頁面表單校驗(yàn)的實(shí)例

    下面小編就為大家?guī)硪黄狫avaScript 完成注冊(cè)頁面表單校驗(yàn)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • 關(guān)于IE只能嵌套27層表格的說法證明

    關(guān)于IE只能嵌套27層表格的說法證明

    關(guān)于IE只能嵌套27層表格的說法證明...
    2006-11-11
  • JavaScript模擬深藍(lán)vs卡斯帕羅夫的國際象棋對(duì)局示例

    JavaScript模擬深藍(lán)vs卡斯帕羅夫的國際象棋對(duì)局示例

    這篇文章主要介紹了JavaScript模擬深藍(lán)vs卡斯帕羅夫的國際象棋對(duì)局示例,使用javascript較為逼真的模擬出了國際象棋對(duì)弈的場景,需要的朋友可以參考下
    2015-04-04
  • JS數(shù)字精度丟失的原因及解決方案

    JS數(shù)字精度丟失的原因及解決方案

    JS的數(shù)字類型一旦數(shù)字超過限值,JS將會(huì)丟失精度,導(dǎo)致前后端的值出現(xiàn)不一致,這篇文章主要給大家介紹了關(guān)于JS數(shù)字精度丟失的原因分析及解決方法,需要的朋友可以參考下
    2022-04-04
  • JavaScript onclick事件使用方法詳解

    JavaScript onclick事件使用方法詳解

    這篇文章主要介紹了JavaScript onclick事件使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • JS 獲取頁面尺寸的方法詳解

    JS 獲取頁面尺寸的方法詳解

    通過 JS 獲取頁面相關(guān)的尺寸是比較常見的操作,尤其是在動(dòng)態(tài)計(jì)算頁面布局時(shí),今天我們就來學(xué)習(xí)一下幾個(gè)獲取頁面尺寸的基本方法,需要的朋友可以參考下
    2023-09-09
  • ajax前臺(tái)后臺(tái)跨域請(qǐng)求處理方式

    ajax前臺(tái)后臺(tái)跨域請(qǐng)求處理方式

    本篇文章通過前臺(tái)跨域請(qǐng)求處理以及后臺(tái)跨域的數(shù)據(jù)處理方式介紹,詳細(xì)分析了ajax跨域的問題,對(duì)此有需要的朋友學(xué)習(xí)下。
    2018-02-02

最新評(píng)論

门源| 龙山县| 达州市| 嘉峪关市| 阜新市| 望江县| 易门县| 和政县| 东山县| 通州市| 双鸭山市| 平乐县| 马尔康县| 金昌市| 从化市| 巴彦淖尔市| 白山市| 安多县| 莱州市| 磐安县| 迭部县| 鄂州市| 晋江市| 云龙县| 临泉县| 长兴县| 桑日县| 二连浩特市| 通化县| 株洲县| 大安市| 杭州市| 兴山县| 汶川县| 大关县| 昔阳县| 从化市| 凌云县| 青海省| 涿鹿县| 长宁区|