html的一个选修课作业!!求帮助!

求大神.....就是把代码给我就行,不用做出来现成的网页...只要代码敲到高级记事本上给老师就行了...谢谢!!可以的话给邮箱[email protected]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.a { width: 150px; height: 300px; background-color: yellow; }
</style>
<script>
window.onload = function(){
window.document.onkeydown = function(evt){
evt = (evt) ? evt : window.event;
switch(evt.keyCode) {
case 82: //r
document.getElementById("a").style.backgroundColor = "red";
break;
case 89: //y
document.getElementById("a").style.backgroundColor = "yellow";
break;
case 71: //g
document.getElementById("a").style.backgroundColor = "green";
break;
case 66: //b
document.getElementById("a").style.backgroundColor = "black";
break;
}
};
}
</script>
</head>
<body>
<div class="a" id="a"></div>
</body>
</html>

追问

啊!谢谢!不过想问一下,后面那个小题的代码有没有...

追答

啥意思?

追问

就是要一个【按向左把图形变红色,按向右把图形变蓝色】这两个步骤的代码...

追答

哦。稍等,修改代码。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.a { position:absolute; width: 150px; height: 300px; background-color: yellow; }
</style>
<script>
window.onload = function(){
    window.document.onkeydown = function(evt){
        evt = (evt) ? evt : window.event;
var obj = document.getElementById("a");
        switch(evt.keyCode) {
            case 82: //r
            ....
            case 66: //b
               obj.style.backgroundColor = "black";
                break;
            case 37: //left
var _left = parseInt(obj.style.left.replace("px", ""));
                obj.style.left = (_left - 10) + "px";
                break;
            case 39: //right
var _left = parseInt(obj.style.left.replace("px", ""));
                obj.style.left = (_left + 10) + "px";
                break;
        }
    };
}
</script>
</head>
<body>
<div class="a" id="a" style="left: 10px;"></div>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜