如何在点击时获得声音,并在计算器显示屏上显示7



document.getElementById("a").onclick = function(){
	var a_toets = document.getElementById("audio_a");
	a_toets.play();
}
document.addEventListener('keydown', function(event) {
switch(event.keyCode) {
		case 55:  var a_toets = document.getElementById("audio_a");
		          a_toets.play();
		break;
	}
})
var inputLabel = document.getElementById('inputLabel');
function pushBtn(obj) {
var pushed = obj.innerHTML;
if (pushed == '=') {
//berekenen
inputLabel.innerHTML = eval(inputLabel.innerHTML);

} else if (pushed == 'AC') {
//alles weg
inputLabel.innerHTML = '0';
} else {
if (inputLabel.innerHTML == '0') {
inputLabel.innerHTML = pushed;
} else {
inputLabel.innerHTML += pushed;
}
}
}
@charset "utf-8";
* {
font-family: 'Inconsolata', monospace;
color: #555;
}
body {
background-color: #3fb399;
}
.container {
width: 320px;
background-color: white;
margin: 120px auto;
}
table {
width: 100%;
border-color: #f4f4f4;
}
td {
width: 25%;
}
button {
width: 100%;
height: 70px;
font-size: 24px;
background-color: white;
border: none;
}
#inputLabel {
height: 120px;
font-size: 40px;
vertical-align: bottom;
text-align: right;
padding-right: 16px;
background-color: #ececec;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rekenmachine</title>
<link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<table border="1" cellspacing="0">
<tr>
<td colspan="4" id="inputLabel">0</td>
</tr>
<tr>
<td colspan="3"><button onclick="pushBtn(this);">AC</button></td>
<td><button onclick="pushBtn(this);">/</button></td>
</tr>
<tr>
<td><button id="a" onclick="pushBtn(this);">7</button></td>
<td><button onclick="pushBtn(this);">8</button></td>
<td><button onclick="pushBtn(this);">9</button></td>
<td><button onclick="pushBtn(this);">*</button></td>
</tr>
<tr>
<td><button onclick="pushBtn(this);">4</button></td>
<td><button onclick="pushBtn(this);">5</button></td>
<td><button onclick="pushBtn(this);">6</button></td>
<td><button onclick="pushBtn(this);">-</button></td>
</tr>
<tr>
<td><button onclick="pushBtn(this);">1</button></td>
<td><button onclick="pushBtn(this);">2</button></td>
<td><button onclick="pushBtn(this);">3</button></td>
<td><button onclick="pushBtn(this);">+</button></td>
</tr>
<tr>
<td colspan="2"><button onclick="pushBtn(this);">0</button></td>
<td><button onclick="pushBtn(this);">.</button></td>
<td><button onclick="pushBtn(this);">=</button></td>
</tr>
</table>
</div>
<audio id="audio_a">
<source src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-18146/zapsplat_multimedia_click_001_19367.mp3?_=1" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script src="js/script.js"></script>
</body>
</html>

我必须用html-css-js做一个计算器。我有这个计算器,它运行得很好。但现在我在一个键上添加了一个声音(mp3链接(。当你点击按键时,声音就响了。但这把钥匙不再是计算器了。因此该键不会显示在计算器显示器上。所以你不能用那个键langer计算。我该怎么解决?

我希望你能帮我。我想要一个声音在键上,我希望它像以前一样作为一个普通的计算器工作。这是我的html和js

pushBtn函数中播放音频:

var a_toets = document.getElementById("audio_a");
var inputLabel = document.getElementById('inputLabel');
function pushBtn(obj) {
	  a_toets.play();
var pushed = obj.innerHTML;
if (pushed == '=') {
//berekenen
inputLabel.innerHTML = eval(inputLabel.innerHTML);

} else if (pushed == 'AC') {
//alles weg
inputLabel.innerHTML = '0';
} else {
if (inputLabel.innerHTML == '0') {
inputLabel.innerHTML = pushed;
} else {
inputLabel.innerHTML += pushed;
}
}
}
@charset "utf-8";
* {
font-family: 'Inconsolata', monospace;
color: #555;
}
body {
background-color: #3fb399;
}
.container {
width: 320px;
background-color: white;
margin: 120px auto;
}
table {
width: 100%;
border-color: #f4f4f4;
}
td {
width: 25%;
}
button {
width: 100%;
height: 70px;
font-size: 24px;
background-color: white;
border: none;
transition-duration:0.3s;
}
button:hover {
background-color: #a4dfff;
}
button:active {
background-color: #30ff62;
}
#inputLabel {
height: 120px;
font-size: 40px;
vertical-align: bottom;
text-align: right;
padding-right: 16px;
background-color: #ececec;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rekenmachine</title>
<link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<table border="1" cellspacing="0">
<tr>
<td colspan="4" id="inputLabel">0</td>
</tr>
<tr>
<td colspan="3"><button onclick="pushBtn(this);">AC</button></td>
<td><button onclick="pushBtn(this);">/</button></td>
</tr>
<tr>
<td><button onclick="pushBtn(this);">7</button></td>
<td><button onclick="pushBtn(this);">8</button></td>
<td><button onclick="pushBtn(this);">9</button></td>
<td><button onclick="pushBtn(this);">*</button></td>
</tr>
<tr>
<td><button onclick="pushBtn(this);">4</button></td>
<td><button onclick="pushBtn(this);">5</button></td>
<td><button onclick="pushBtn(this);">6</button></td>
<td><button onclick="pushBtn(this);">-</button></td>
</tr>
<tr>
<td><button onclick="pushBtn(this);">1</button></td>
<td><button onclick="pushBtn(this);">2</button></td>
<td><button onclick="pushBtn(this);">3</button></td>
<td><button onclick="pushBtn(this);">+</button></td>
</tr>
<tr>
<td colspan="2"><button onclick="pushBtn(this);">0</button></td>
<td><button onclick="pushBtn(this);">.</button></td>
<td><button onclick="pushBtn(this);">=</button></td>
</tr>
</table>
</div>
<audio id="audio_a">
<source src="https://www.zapsplat.com/wp-content/uploads/2015/sound-effects-18146/zapsplat_multimedia_click_001_19367.mp3?_=1" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script src="js/script.js"></script>
</body>
</html>

最新更新