久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

javascript - 請問w3c里面這個js案例,我用window.onload來寫怎么點擊結束按鈕之后,再點擊開始,從0計數

瀏覽:112日期:2023-09-23 13:32:44

問題描述

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Document</title> <script> window.onload = function() { var a = 0; obtn1 = document.getElementById('btn1'); obtn2 = document.getElementById('btn2'); obtn1.onclick = function() {setTimeout(function rec() { document.getElementById('txt').value = a; a = a + 1; x = setTimeout(rec, 1000)}, 1000) } obtn2.onclick = function() {var b = 0;setTimeout(document.getElementById(’txt’).value=0, 0); clearTimeout(x);} } </script></head><body> <input type='text' name='text' /> <input type='button' value='開始計時!' /> <input type='button' value='結束計時!' /></body></html>

這是我自己學著寫的,請問哪里出問題了,點擊結束之后,是接著原來的數字計數的,我希望再點擊開始的時候,從0開始。

問題解答

回答1:

obtn2.onclick = function() {a = 0; //需要重新重置開始數字document.getElementById(’txt’).value=0; clearTimeout(x);} }回答2:

點擊結束重加載一下頁面??!

回答3:

window.onload=function(){ var a = 0;

obtn1 = document.getElementById('btn1');obtn2 = document.getElementById('btn2');txt=document.getElementById(’txt’);obtn1.onclick=function(){txt.value=++a;}obtn2.onclick=function(){a=0;txt.value=a;}

}

回答4:

把var a = 0;放到obtn1.onclick這個函數里聲明就好了

標簽: JavaScript
主站蜘蛛池模板: 讷河市| 盐池县| 宜君县| 黔西县| 馆陶县| 庆云县| 龙岩市| 古丈县| 雅安市| 炎陵县| 金山区| 深水埗区| 长顺县| 云安县| 浑源县| 天门市| 福泉市| 伊吾县| 武宁县| 吴川市| 大埔区| 清河县| 临澧县| 勐海县| 乃东县| 华池县| 城步| 额济纳旗| 濮阳市| 景泰县| 文化| 若羌县| 博湖县| 金川县| 中方县| 天镇县| 纳雍县| 濉溪县| 桃园市| 江西省| 双鸭山市|