補(bǔ)充以下HTML乎折,實(shí)現(xiàn)點(diǎn)擊某一個(gè)數(shù)字可以alert出該數(shù)字绒疗。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件監(jiān)聽</title>
</head>
<body>
<ul id="no">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
<script>
// write your code here
// ......
</script>
</body>
</html>
答案:
var list=document.getElementById('no').getElementsByTagName('li');
for(var i=0;i<list.length;i++){
list[i].onclick=function(){
alert(this.firstChild.nodeValue);//使用dom的nodeValue屬性
}
}
實(shí)現(xiàn)一個(gè)基礎(chǔ)的計(jì)時(shí)器應(yīng)用,點(diǎn)擊開始按鈕開始計(jì)時(shí)骂澄,點(diǎn)擊結(jié)束按鈕結(jié)束計(jì)時(shí)忌堂,并把計(jì)時(shí)的結(jié)果顯示在上面的輸入框中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>定時(shí)器示例</title>
</head>
<body>
<input type="text" id="result">
<input type="button" value="開始" >
<input type="button" value="結(jié)束" >
<script>
// write your code here
// ......
</script>
</body>
</html>
答案
var hello=0;
var time;
var go=0;
function timedCount(){
document.getElementById('result').value=hello;
hello=hello+1;
time=setTimeout(function(){timedCount()},1000);
}
function start(){
if (!go){
go=1;
timedCount();
}
}
function stop(){
clearTimeout(time);
go=0;
}