国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

js腳本編寫簡單刷票投票系統(tǒng)

2024-05-06 16:38:06
字體:
供稿:網(wǎng)友

本文實例為大家分享了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é)點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é)點}}}return classArr;}function delete_FF(element) { // 在FireFox中刪除子節(jié)點為空的元素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é)點}}}window.onload = function() {var persons = getElemensByClassName("person");// alert(persons);for(var item in persons) { //遍歷所有person,為它們綁定投票事件(function(_item) { //匿名函數(shù)傳入item, 防止因作用域問題導(dǎo)致item總為最后一個delete_FF(persons[_item]); //出去FF中空行代表的子節(jié)點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") { //投票結(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ù)num傳送給服務(wù)器保存,更新時也是和服務(wù)器中的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改變則 觸發(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í)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长汀县| 临潭县| 吉木萨尔县| 名山县| 开阳县| 常德市| 无锡市| 社旗县| 越西县| 巴林左旗| 株洲市| 弥渡县| 涟水县| 分宜县| 梓潼县| 张家界市| 基隆市| 嵩明县| 呼玛县| 阳曲县| 扶风县| 孟州市| 武义县| 弥渡县| 梅河口市| 黄龙县| 云南省| 营口市| 梓潼县| 东城区| 静宁县| 宜兰市| 仲巴县| 林口县| 酉阳| 嘉峪关市| 乡宁县| 建阳市| 淮滨县| 石狮市| 赤壁市|