欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實現(xiàn)抽獎的兩種方法

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

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

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

方法一:使用table寫一個隨機抽獎

這是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>抽獎</title>
</head>
 
<body>
 <div class="content">
  <div class="top">
   抽獎活動
  </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>美團</th>
     <th>樂視</th>
     <th>小米</th>
     <th>網(wǎng)易</th>
     <th>酷我</th>
    </tr>
    <tr>
     <th>愛奇藝</th>
     <th>盛大</th>
     <th>短文學</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() {
  // 啟動定時器
  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標簽寫

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>抽獎</title>
</head>
 
<body>
 <div class="content">
  <div class="top">
   抽獎活動
  </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");
 // 動態(tài)添加span
 for (var i = 0; i < 25; i++) {
  // 創(chuàng)建一個新的標簽
  var el = document.createElement("span");
  // 給標簽設置內(nèi)容
  el.innerText = i;
  // 添加子元素
  div.appendChild(el);
 }
 
 var timer;
 var button = document.querySelector("#button");
 function toStart() {
  // 啟動定時器
  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>

兩個頁面的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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解iframe與frame的區(qū)別

    詳解iframe與frame的區(qū)別

    Frame與Iframe兩者可以實現(xiàn)的功能基本相同,不過Iframe比Frame具有更多的靈活性,通過本文給大家詳解細節(jié)iframe與frame的區(qū)別,對iframe與frame的區(qū)別相關知識感興趣的朋友一起學習吧
    2016-01-01
  • JS獲取鼠標坐標的實例方法

    JS獲取鼠標坐標的實例方法

    這篇文章介紹了JS獲取鼠標坐標的實例方法,有需要的朋友可以參考一下
    2013-07-07
  • javascript中數(shù)組與對象的使用方法區(qū)別

    javascript中數(shù)組與對象的使用方法區(qū)別

    數(shù)組(array)是按次序排列的一組值。JS其實沒有真正的數(shù)組,只是用對象模擬數(shù)組。本質上,數(shù)組屬于一種特殊的對象。typeof運算符會返回數(shù)組的類型是object。在javascript中,數(shù)組又可以認為是索引數(shù)組,即可以用整數(shù)來進行索引。數(shù)組和對象在這種情況下非常接近。
    2022-12-12
  • JS實現(xiàn)全屏的四種寫法

    JS實現(xiàn)全屏的四種寫法

    這篇文章主要為大家詳細介紹了JS或jquery實現(xiàn)全屏的四種寫法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • JS 數(shù)字轉換為大寫金額的簡單實例

    JS 數(shù)字轉換為大寫金額的簡單實例

    下面小編就為大家?guī)硪黄狫S 數(shù)字轉換為大寫金額的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • js調用打印機打印網(wǎng)頁字體總是縮小一號的解決方法

    js調用打印機打印網(wǎng)頁字體總是縮小一號的解決方法

    直接調用window.print(),但是打印出來后,字體總是縮小一號,后來直接target="_blank",就可以正常打印了,下面是實現(xiàn)代碼
    2014-01-01
  • 獲取dom元素那些討厭的位置封裝代碼

    獲取dom元素那些討厭的位置封裝代碼

    你還在為offset,scroll,inner,client,screen,mouse發(fā)愁嗎? 頭都大了。而且各個瀏覽器方法還不太一樣。所以往往很浪費我們的時間。 于是我產(chǎn)生了寫一個方法用戶 專門獲取這樣的數(shù)據(jù)。
    2010-06-06
  • ES6 Generator函數(shù)的應用實例分析

    ES6 Generator函數(shù)的應用實例分析

    這篇文章主要介紹了ES6 Generator函數(shù)的應用,結合實例形式分析了ES6 Generator函數(shù)異步操作與異常捕獲相關使用技巧,需要的朋友可以參考下
    2019-06-06
  • ng-options和ng-checked在表單中的高級運用(推薦)

    ng-options和ng-checked在表單中的高級運用(推薦)

    AngularJS是當前非常的流行的前端框架,它的語法糖非常多,也極大的方便了前端開發(fā)者。這篇文章主要介紹了ng-options和ng-checked在表單中的高級運用,需要的朋友可以參考下
    2017-01-01
  • JavaScript 模擬類機制及私有變量的方法及思路

    JavaScript 模擬類機制及私有變量的方法及思路

    這篇文章介紹了JavaScript模擬類機制及私有變量的方法及思路,有需要的朋友可以參考一下
    2013-07-07

最新評論