課程
/前端開發(fā)
/JavaScript
/DOM事件探秘
請問老師,如果,我第一次點擊開始,但是我結(jié)束的時候想用enter點擊,這種情況又該如何判斷呢?
2014-07-02
源自:DOM事件探秘 4-5
正在回答
<!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>
<style>
*{margin:0; padding:0;}
#title{
width:400px;
height:60px;
margin:0 auto;
padding-top:30px;
font-size:24px;
font-weight:blod;
line-height:60px;
text-align:center;
color:#f00;
}
#btns{
width:204px;
height:30px;
#btns span{
width:80px;
height:28px;
display:block;
background:#036;
float:left;
margin-right:20px; /*注意border的寬度和margin的大小*/
border:1px solid #036;
border-radius:7px;
line-height:28px;
color:#fff;
font-family:"microsoft yahei";
font-size:20px;
cursor:pointer;
</style>
<script>
window.onload = function ()
{
var oTitle = document.getElementById('title');
var oPlay = document.getElementById('play');
var oStop = document.getElementById('stop');
var array = ['iphone6','iWatch','MX4','Canon','iMac','100元充值卡','200元超市購物卡','謝謝參與','蘋果筆記本','智能手環(huán)'];
var timer = null;
var flag = 0; //設置一個標志變量
//鼠標點擊事件
oPlay.onclick = startMove; //為什么此處加()不行
oStop.onclick = stopMove;
//鍵盤事件
document.onkeyup = function (e)
var oEvent = e || event;
if(oEvent.keyCode == 13)
if(flag == 0)
startMove(); //為什么此處非要加()
flag = 1;
else
stopMove();
flag = 0;
function startMove()
clearInterval(timer);
timer = setInterval(function (){
var rand = Math.floor(Math.random()*array.length);
oTitle.innerHTML = array[rand];
}, 50);
oPlay.style.background = '#999';
function stopMove()
if(flag == 1)
oPlay.style.background = '#036';
flag = 0
</script>
</head>
<body>
<div id="title">
開始抽獎啦!
</div>
<div id="btns">
<span id="play">開始</span>
<span id="stop">停止</span>
</body>
</html>
yunxiange
不錯不錯,講的很清晰易懂
把flag=1直接放進playFun()函數(shù)里面,把flag=0直接放進stopFun()函數(shù)里面,應該就可以解決了
同學您好,如果是這樣的話您還是一樣的要得到enter鍵的鍵碼值的!然后根據(jù)這個鍵碼值來結(jié)束計時。
舉報
DOM事件?本課程會通過實例來給小伙伴們講解如何使用這些事件
4 回答
5 回答
1 回答
2 回答
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2014-09-10
<!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>
<style>
*{margin:0; padding:0;}
#title{
width:400px;
height:60px;
margin:0 auto;
padding-top:30px;
font-size:24px;
font-weight:blod;
line-height:60px;
text-align:center;
color:#f00;
}
#btns{
width:204px;
height:30px;
margin:0 auto;
}
#btns span{
width:80px;
height:28px;
display:block;
background:#036;
float:left;
margin-right:20px; /*注意border的寬度和margin的大小*/
border:1px solid #036;
border-radius:7px;
line-height:28px;
text-align:center;
color:#fff;
font-weight:blod;
font-family:"microsoft yahei";
font-size:20px;
cursor:pointer;
}
</style>
<script>
window.onload = function ()
{
var oTitle = document.getElementById('title');
var oPlay = document.getElementById('play');
var oStop = document.getElementById('stop');
var array = ['iphone6','iWatch','MX4','Canon','iMac','100元充值卡','200元超市購物卡','謝謝參與','蘋果筆記本','智能手環(huán)'];
var timer = null;
var flag = 0; //設置一個標志變量
//鼠標點擊事件
oPlay.onclick = startMove; //為什么此處加()不行
oStop.onclick = stopMove;
//鍵盤事件
document.onkeyup = function (e)
{
var oEvent = e || event;
if(oEvent.keyCode == 13)
{
if(flag == 0)
{
startMove(); //為什么此處非要加()
flag = 1;
}
else
{
stopMove();
flag = 0;
}
}
}
function startMove()
{
if(flag == 0)
{
clearInterval(timer);
timer = setInterval(function (){
var rand = Math.floor(Math.random()*array.length);
oTitle.innerHTML = array[rand];
}, 50);
oPlay.style.background = '#999';
}
flag = 1;
}
function stopMove()
{
if(flag == 1)
{
clearInterval(timer);
oPlay.style.background = '#036';
}
flag = 0
}
}
</script>
</head>
<body>
<div id="title">
開始抽獎啦!
</div>
<div id="btns">
<span id="play">開始</span>
<span id="stop">停止</span>
</div>
</body>
</html>
2014-08-09
不錯不錯,講的很清晰易懂
2014-07-23
把flag=1直接放進playFun()函數(shù)里面,把flag=0直接放進stopFun()函數(shù)里面,應該就可以解決了
2014-07-03
同學您好,如果是這樣的話您還是一樣的要得到enter鍵的鍵碼值的!然后根據(jù)這個鍵碼值來結(jié)束計時。