JS简单的投票代码,带无刷新比例显示效果,闲着没事的时候写的,基本效果都实现了,就是不太漂亮。当我们使用鼠标分别点击投票按钮,可看到对应的数据在增加或减少,适时的,可以看作是一个挺简单的Ajax无刷新的应用,分享给朋友们参考。 示例: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>简单的JS投票代码</title> <style> *{ margin:0; padding:0;} #div1{ background:#000; width:500px; height:30px; position:relative;} .a_1, .a_2{ position:absolute; top:0; height:30px;} .a_1{ left:0; background:#0F6;} .a_2{ right:0; background:#39C;} input{ margin-top:30px;} </style> <script> window.onload=function() { var oDiv=document.getElementById('div1'); var aDiv=oDiv.getElementsByTagName('div'); var aBtn=document.getElementsByTagName('input'); var lNum=50; var rNum=25; var lNums=(rNum/(lNum+rNum))*500; var rNums=500-lNums; aDiv[1].style.width=parseInt(lNums)+'px'; aDiv[0].style.width=500-parseInt(lNums)+'px'; aDiv[0].innerHTML=(lNum/(lNum+rNum)).toFixed(2); aDiv[1].innerHTML=(1-lNum/(lNum+rNum)).toFixed(2); aBtn[0].onclick=function() { lNum=lNum+1; var lNumss=parseInt(lNum/(lNum+rNum)*500); aDiv[0].style.width=lNumss+'px'; aDiv[1].style.width=(500-lNumss)+'px'; aDiv[0].innerHTML=(lNum/(lNum+rNum)).toFixed(2); aDiv[1].innerHTML=(1-lNum/(lNum+rNum)).toFixed(2); } aBtn[1].onclick=function() { rNum=rNum+1; var rNumss=parseInt(rNum/(lNum+rNum)*500); aDiv[0].style.width=(500-rNumss)+'px'; aDiv[1].style.width=rNumss+'px'; aDiv[0].innerHTML=(lNum/(lNum+rNum)).toFixed(2); aDiv[1].innerHTML=(1-lNum/(lNum+rNum)).toFixed(2); } } </script> </head> <body> <div id="div1"> <div class="a_1"></div> <div class="a_2"></div> </div> <input type="button" name="veryhuo-com" value="投票1" /> <input type="button" name="veryhuo-com" value="投票2" /> </body> </html> <div style="text-align:center;margin:30px 0 0 0;"><hr style="color:#999;height:1px;">如不能显示效果,请按Ctrl+F5刷新本页,更多网页代码:<a href='http://www.veryhuo.com/' target='_blank'>http://www.veryhuo.com/</a></div> 提示:可修改后代码再运行! 文章导航 《荒野大镖客:救赎2》演示中的疯狂细节:连泥巴都做得这么细网页简繁转换代码,JS无刷新简体繁体切换