如何在 JavaScript 中创建 onmousedown
的函数在特定元素中执行此操作:
document.getElementById('KeyOfC').play();
和
mouseup
执行这个:document.getElementById('KeyOfC').pause();
document.getElementById('null').play();
我很乐意得到任何答案,因为我是 JavaScript 新手。
最佳答案
您可以将这些触发器绑定(bind)到元素(在这种情况下,它具有 id
的 foo
):
document.getElementById('foo').onmousedown = function()
{
document.getElementById('KeyOfC').play();
}
document.getElementById('foo').onmouseup = function()
{
document.getElementById('KeyOfC').pause();
document.getElementById('null').play();
}
如果要指定多个
id
s(来自您的评论),您需要一个 JavaScript 库来执行此操作。我会推荐 jQuery :$('#foo, #bar, #foobar').mousedown(function()
{
$('#KeyOfC').play();
});
$('#foo, #bar, #foobar').mouseup(function()
{
$('#KeyOfC').pause();
$('#null').play();
});
如果你做过 CSS,你可以使用 CSS 规则作为 jQuery Selectors ,如
.ClassName, #your_id, etc.
.
关于javascript - onmousedown 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421625/