<!-- lang: html --> <!DOCTYPE html> <!-- lang: html --> <html> <!-- lang: html --> <head> <!-- lang: html --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- lang: html --> <script type="text/javascript"> <!-- lang: html --> <!-- lang: html --> window.onload = function () { <!-- lang: html --> function Timenow() { <!-- lang: html --> this.start1 = 0; <!-- lang: html --> this.end1 = 0; <!-- lang: html --> this.sum = 0; <!-- lang: html --> } <!-- lang: html --> <!-- lang: html --> Timenow.prototype.start =function() { <!-- lang: html --> this.start1 = parseInt((new Date()).getTime()); <!-- lang: html --> document.getElementById("txt").value=this.start1; <!-- lang: html --> }; <!-- lang: html --> <!-- lang: html --> Timenow.prototype.end =function () { <!-- lang: html --> this.end1 = parseInt((new Date()).getTime()) - this.start1; <!-- lang: html --> this.sum += this.end1; <!-- lang: html --> document.getElementById("txt").value=this.sum; <!-- lang: html --> }; <!-- lang: html --> <!-- lang: html --> var test = new Timenow(); <!-- lang: html --> var test1 = new Timenow(); <!-- lang: html --> <!-- lang: html --> document.getElementsByClassName("test")[0].onmouseover = function() {test.start()}; <!-- lang: html --> document.getElementsByClassName("test")[0].onmouseout = function() {test.end()}; <!-- lang: html --> <!-- lang: html --> document.getElementsByClassName("test")[1].onmouseover = function() {test1.start()}; <!-- lang: html --> document.getElementsByClassName("test")[1].onmouseout = function() {test1.end()}; <!-- lang: html --> } <!-- lang: html --> </script> <!-- lang: html --> </head> <!-- lang: html --> <body > <!-- lang: html --> <form > <!-- lang: html --> <div class="test" style="background-color:#00ff00;height:400px;width:400px" > <!-- lang: html --> <div></div> <!-- lang: html --> <div></div> <!-- lang: html --> </div> <!-- lang: html --> <div class="test" style="background-color:#ff0000;height:400px;width:400px" > <!-- lang: html --> <div></div> <!-- lang: html --> <div></div> <!-- lang: html --> </div> <!-- lang: html --> <input type="button" value="開始計時!" > <!-- lang: html --> <input type="text" id="txt"> <!-- lang: html --> </form> <!-- lang: html --> <!-- lang: html --> </body> <!-- lang: html --> </html>
雖然糾結了一段時間,http://pan.baidu.com/s/1kTzajB5?qq-pf-to=pcqq.c2c這裏面的第6題,仍是有了一個比較初步的答案。javascript
下一步要糾結的問題是,如何給div添加自定義的屬性。以及關於毫秒的計算。html