您好,登錄后才能下訂單哦!
使用JavaScript怎么編寫一個隨機(jī)點(diǎn)名程序?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
<!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"> <title>隨機(jī)點(diǎn)名</title> <style> #content{ width: 600px; height: 400px; background: #000; margin: 100px auto; text-align: center; position: relative; line-height: 300px; color: dodgerblue; font-size: 70px; } #btn1{ background: #ccc; width: 180px; height: 80px; font-size: 30px; color: #f40; border-radius: 12px; position: absolute; bottom: 30px; left: 50%; margin-left: -90px; } </style> </head> <body> <div id="content"> <span id="span1"> 點(diǎn)擊開始 </span> <button id="btn1"> start </button> </div> <script> var arr = ['中國','英國','德國','美國','意大利','法國','西班牙','日本','阿聯(lián)酋','荷蘭','葡萄牙']; var $btn1 = document.getElementById('btn1'); var $content = document.getElementById('content'); var $span1 = document.getElementById('span1'); var timer;//計(jì)時器 var testNum = true; $btn1.onclick = function(){ if(testNum){ // console.log(1); start(); $btn1.innerHTML = 'stop'; testNum = false; } else{ // console.log(0); stop(); $btn1.innerHTML = 'start'; testNum = true; } } function start(){ timer = setInterval(function(){ var num = random(0,arr.length - 1); $span1.innerHTML = arr[num]; },50) } function stop(){ clearInterval(timer); } // 隨機(jī)函數(shù) function random(a,b){ var randomNum = Math.round(Math.random() * (b - a) + a); return randomNum; } </script> </body> </html>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。