DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>title>
<style>
.wrap {
width: 400px;
height: 400px;
margin: 100px auto 0;
}
.wrap h1 {
text-align: center;
}
.wrap div {
width: 400px;
height: 300px;
background: pink;
font-size: 30px;
text-align: center;
line-height: 300px;
}
style>
head>
<body>
<div class="wrap">
<h1>提示: 通过 按键 改变颜色h1>
<div id="bgChange">
keyCode为:
<span id="keyCodeSpan">span>
div>
div>
<script src="jquery-1.12.4.js">script>
<script>
$(function () {
var $div = $('#bgChange');
var $showCode = $('#keyCodeSpan');
$(document).on('keydown', function (e) {
switch (e.keyCode) {
case 82:
$div.css('backgroundColor', 'red');
$showCode.text(82);
break;
case 71:
$div.css('backgroundColor', 'green');
$showCode.text(71);
break;
case 66:
$div.css('backgroundColor', 'blue');
$showCode.text(66);
break;
case 80:
$div.css('backgroundColor', 'purple');
$showCode.text(80);
break;
case 89:
$div.css('backgroundColor', 'yellow');
$showCode.text(89);
break;
default:
$div.css('backgroundColor', 'pink');
$showCode.text("查无此键");
break;
}
});
});
script>
body>
html>

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
