当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js脚本编写简单刷票投票系统

js脚本编写简单刷票投票系统

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了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)); //赋上id

var 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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网