假设我现在有6个button(数量不定)
请问如何实现如下效果:
1.当我双击其中任一一个按钮时,其它button均为灰色不可用状态
2.同时,应该双击的button的value开始变成从双击开始到现在的时间,且为自动刷新
3.再次双击时,停止计时并其它button恢复为可用状态。
想了一些方法,但总感觉不太理想,特别如何进行计时,感觉没什么好的办法。
请高手指点一下,不胜感激!
------解决方案--------------------
计时
http://www.w3school.com.cn/htmldom/met_win_setinterval.asp
另外在双击事件时传一个this参数会使代码简化
------解决方案--------------------
- HTML code
<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> var timer = 0, t; function timing(buttonID) { var now = new Date(); now = Math.round(parseInt(now.getTime() / 1000)); document.getElementById(buttonID).value = now - timer; } window.onload = function() { var obj = document.getElementById('demo').getElementsByTagName('input'); for (var i = 0; i < obj.length; i ++) { obj[i].ondblclick = function() { for (var j = 0; j < obj.length; j ++) obj[j].disabled = true; this.disabled = false; if (this.value == this.id) { timer = new Date(); timer = Math.round(parseInt(timer.getTime()) / 1000); t = window.setInterval('timing("' + this.id + '")', 1000); } else { window.clearInterval(t); for (var j = 0; j < obj.length; j ++) obj[j].disabled = false; this.value = this.id; } } } } </script> </head> <body> <form id="demo"> <input type="button" id="BUTTON1" value="BUTTON1" /> <input type="button" id="BUTTON2" value="BUTTON2" /> <input type="button" id="BUTTON3" value="BUTTON3" /> <input type="button" id="BUTTON4" value="BUTTON4" /> <input type="button" id="BUTTON5" value="BUTTON5" /> <input type="button" id="BUTTON6" value="BUTTON6" /> </form> </body> </html>