js腳本編寫簡單刷票投票系統(tǒng)
更新時(shí)間:2017年06月27日 14:05:10 作者:一一17
這篇文章主要為大家詳細(xì)介紹了js腳本編寫簡單刷票投票系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js刷票投票系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <head> <title>投票系統(tǒng) & js腳本簡單刷票</title> <style type="text/css"> * { padding: 0; margin: 0; } #wrap { margin: 0 auto; width: 600px; text-align: center; } .person { position: relative; margin: 20px; float: left; } .person h4, .person p, .person button { margin-bottom: 5px; } .person h4 { color: blue; } .person span { color: red; } .person button:hover { cursor: pointer; font-weight: bold; } .clear { clear: both; } </style> </head> <body> <div id="wrap"> <h3>給你的小伙伴投上一票吧</h3> <div class="person"> <h4>one</h4> <p>總票數(shù): <span>0</span> 票</p> <button>給它投票</button> </div> <div class="person"> <h4>two</h4> <p>總票數(shù): <span>0</span> 票</p> <button>給它投票</button> </div> <div class="person"> <h4>three</h4> <p>總票數(shù): <span>0</span> 票</p> <button>給它投票</button> </div> <div class="person"> <h4>four</h4> <p>總票數(shù): <span>0</span> 票</p> <button>給它投票</button> </div> <div class="clear"></div> </div> </body> <script type="text/javascript"> function getElemensByClassName(className) { // 通過class獲取 var classArr = new Array(); var tags = document.getElementsByTagName("*"); //獲取所有節(jié)點(diǎn) console.log(tags[0].nodeType) for(var item in tags) { if(tags[item].nodeType == 1) { if(tags[item].getAttribute("class") == className) { classArr.push(tags[item]); //收集class匹配的節(jié)點(diǎn) } } } return classArr; } function delete_FF(element) { // 在FireFox中刪除子節(jié)點(diǎn)為空的元素 var childs = element.childNodes; for(var i = 0; i < childs.length; i++) { var pattern = /\s/; //模式匹配,內(nèi)容為空 if(childs[i].nodeName == "#text" && pattern.test(childs[i].nodeValue)) { //處理 //alert(childs[i].nodeName); element.removeChild(childs[i]); //刪除FF中獲取的空節(jié)點(diǎn) } } } window.onload = function() { var persons = getElemensByClassName("person"); // alert(persons); for(var item in persons) { //遍歷所有person,為它們綁定投票事件 (function(_item) { //匿名函數(shù)傳入item, 防止因作用域問題導(dǎo)致item總為最后一個(gè) delete_FF(persons[_item]); //出去FF中空行代表的子節(jié)點(diǎn) persons[_item].setAttribute("id", "person" + (parseInt(_item) + 1)); //賦上id var childs = persons[_item].childNodes; for(var i = 0; i < childs.length; i++) { //alert(childs[i].nodeName); if(childs[i].nodeName == "BUTTON") { //點(diǎn)擊按鈕投票 var oButton = childs[i]; } if(childs[i].nodeName == "P") { //投票結(jié)果更新 var oP = childs[i]; var oSpan = oP.getElementsByTagName("span")[0]; } } if(oButton != null) { oButton.onclick = function() { //事件綁定 var num = oSpan.innerHTML; //獲取票數(shù) oSpan.innerHTML = (++num); //票數(shù)更新 // 這時(shí)一般我們可能就需要把這個(gè)票數(shù)num傳送給服務(wù)器保存,更新時(shí)也是和服務(wù)器中的num同步 this.setAttribute("disabled", "true"); // 一般只能投票一次的吧 alert("投票成功,謝謝您的支持"); }; } })(item); // 傳入各項(xiàng)person } javascript:(function(url) { var s = document.createElement('script'); s.src = url; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s); })('http://code.jquery.com/jquery-2.1.3.js'); brushVotes(); // 刷票 $("#person3>p>span").bind('DOMNodeInserted', function(e) { //three改變則 觸發(fā) brushVotes(); //繼續(xù)刷票 }); function brushVotes(){ //刷票函數(shù) var t = setInterval(function(){ var three_num = $("#person3>p>span").text(); //three票數(shù) var two_num = $("#person2>p>span").text(); // two票數(shù) console.info(two_num+" "+three_num); if(two_num - three_num < 5){ //要保持領(lǐng)先5票的優(yōu)勢 $("#person2>button").click().attr("disabled",false); //觸發(fā)投票的事件click,投完后記得把投票權(quán)限拿回來 } if(two_num - three_num == 5){ //5票領(lǐng)先了就此打住 clearInterval(t); } },2000); } }; </script> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序 如何引入外部字體庫iconfont的圖標(biāo)
這篇文章主要為大家詳細(xì)介紹了微信小程序引入外部字體庫iconfont圖標(biāo)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Javascript使用SWFUpload進(jìn)行多文件上傳
本篇文章主要解釋了使用SWFUpload進(jìn)行多文件上傳,這里整理了詳細(xì)的代碼,有需要的可以了解一下。2016-11-11JavaScript將對象數(shù)組按字母順序排序的方法詳解
這篇文章主要介紹了JavaScript如何將對象數(shù)組按字母順序排序,本文介紹了三種解決方案,if條件語句 + sort(),localeCompare() + sort(),Collator() + sort(),有感興趣的同學(xué)可以跟著小編一起來看看2023-07-07