本文實例為大家分享了js刷票投票系統的具體代碼,供大家參考,具體內容如下
<!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>投票系統 & 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>總票數: <span>0</span> 票</p><button>給它投票</button></div><div class="person"><h4>two</h4><p>總票數: <span>0</span> 票</p><button>給它投票</button></div><div class="person"><h4>three</h4><p>總票數: <span>0</span> 票</p><button>給它投票</button></div><div class="person"><h4>four</h4><p>總票數: <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("*"); //獲取所有節點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匹配的節點}}}return classArr;}function delete_FF(element) { // 在FireFox中刪除子節點為空的元素var childs = element.childNodes;for(var i = 0; i < childs.length; i++) {var pattern = //s/; //模式匹配,內容為空if(childs[i].nodeName == "#text" && pattern.test(childs[i].nodeValue)) { //處理//alert(childs[i].nodeName);element.removeChild(childs[i]); //刪除FF中獲取的空節點}}}window.onload = function() {var persons = getElemensByClassName("person");// alert(persons);for(var item in persons) { //遍歷所有person,為它們綁定投票事件(function(_item) { //匿名函數傳入item, 防止因作用域問題導致item總為最后一個delete_FF(persons[_item]); //出去FF中空行代表的子節點persons[_item].setAttribute("id", "person" + (parseInt(_item) + 1)); //賦上idvar childs = persons[_item].childNodes;for(var i = 0; i < childs.length; i++) {//alert(childs[i].nodeName);if(childs[i].nodeName == "BUTTON") { //點擊按鈕投票var oButton = childs[i];}if(childs[i].nodeName == "P") { //投票結果更新var oP = childs[i];var oSpan = oP.getElementsByTagName("span")[0];}}if(oButton != null) {oButton.onclick = function() { //事件綁定var num = oSpan.innerHTML; //獲取票數oSpan.innerHTML = (++num); //票數更新// 這時一般我們可能就需要把這個票數num傳送給服務器保存,更新時也是和服務器中的num同步this.setAttribute("disabled", "true"); // 一般只能投票一次的吧alert("投票成功,謝謝您的支持");};}})(item); // 傳入各項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改變則 觸發brushVotes(); //繼續刷票});function brushVotes(){ //刷票函數var t = setInterval(function(){var three_num = $("#person3>p>span").text(); //three票數var two_num = $("#person2>p>span").text(); // two票數console.info(two_num+" "+three_num);if(two_num - three_num < 5){ //要保持領先5票的優勢$("#person2>button").click().attr("disabled",false); //觸發投票的事件click,投完后記得把投票權限拿回來}if(two_num - three_num == 5){ //5票領先了就此打住clearInterval(t);}},2000);}}; </script></html>以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答