yab*_*zey 8 html javascript audio
有没有人知道是否有任何预定义的方法来为键盘输入HTML表格中的每个字母播放声音?
例如:如果我输入Y,则在文本字段中,网站显示Y等.
或者,最好的方法是什么?
Den*_*ret 20
它很容易播放声音,并且很容易为按键添加处理程序,但是没有预定义的方法来链接这两个操作,因此您必须键入自己的代码.
1)按键操作
document.onkeydown = function() {
...
Run Code Online (Sandbox Code Playgroud)
2)播放声音
添加音频元素:
<audio id=alarm>
<source src=sound/zbluejay.wav>
</audio>
Run Code Online (Sandbox Code Playgroud)
并执行它
document.getElementById('alarm').play();
Run Code Online (Sandbox Code Playgroud)
例如,您可以构建一个将键码链接到声音元素ID的地图:
var sounds = {
88 : 'alarm', // key 'x'
...
};
document.onkeydown = function(e) {
var soundId = sounds[e.keyCode];
if (soundId) document.getElementById(soundId).play();
else console.log("key not mapped : code is", e.keyCode);
}
Run Code Online (Sandbox Code Playgroud)
你可能会在这里找到密码