为什么js keydown对象不起作用
js代码:<scripttype="text/javascript">functionchangeColor(){if(event.altKey){document.ge...
js代码:
<script type="text/javascript"> function changeColor(){ if(event.altKey){ document.getElementById("colorBox").style.backgroundColor = "#00FFFF"; }else if(event.shiftKey){ document.getElementById("colorBox").style.backgroundColor = "#DC143C"; }else if(event.ctrlKey){ document.getElementById("colorBox").style.backgroundColor = "#CC66CC"; }else{ document.getElementById("colorBox").style.backgroundColor = "#990033"; } } document.onkeydown = changeColor; </script>
html代码:
<body> <div id="main"> <div id="colorBox"></div> <div id="mess">按Alt:水蓝 按shift:猩红 按Ctrl:淡紫 <div> </div></body>
按三个键都没有反应: 展开
<script type="text/javascript"> function changeColor(){ if(event.altKey){ document.getElementById("colorBox").style.backgroundColor = "#00FFFF"; }else if(event.shiftKey){ document.getElementById("colorBox").style.backgroundColor = "#DC143C"; }else if(event.ctrlKey){ document.getElementById("colorBox").style.backgroundColor = "#CC66CC"; }else{ document.getElementById("colorBox").style.backgroundColor = "#990033"; } } document.onkeydown = changeColor; </script>
html代码:
<body> <div id="main"> <div id="colorBox"></div> <div id="mess">按Alt:水蓝 按shift:猩红 按Ctrl:淡紫 <div> </div></body>
按三个键都没有反应: 展开
1个回答
展开全部
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RunJS 演示代码</title>
<style>
* {
margin: 0;
padding: 0;
}
#main {
margin: auto;
width: 300px;
}
#colorBox {
background-color: #990033;
width: 100px;
height: 100px;
margin: 90px;
}
#mess {
border: 1px solid #fd3;
}
</style>
<script type="text/javascript">
function changeColor(e) {
e = e || window.event;
if (e.altKey) {
colorBox.style.backgroundColor = "#00FFFF";
} else if (e.shiftKey) {
colorBox.style.backgroundColor = "#DC143C";
} else if (e.ctrlKey) {
colorBox.style.backgroundColor = "#CC66CC";
} else {
colorBox.style.backgroundColor = "#990033";
}
}
document.onkeydown = changeColor;
</script>
</head>
<body>
<div id="main">
<div id="colorBox"></div>
<div id="mess">按Alt:水蓝 按shift:猩红 按Ctrl:淡紫</div>
</div>
</body>
</html>
追问
谢谢,可以了。请问 e = e || window.event;这一句是什么意思?还有后面e.altKey和event.altKey的区别在哪?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询