如何在 JavaScript 中创建一个onmousedown
在特定元素中执行的函数:
document.getElementById('KeyOfC').play();
并mouseup
执行此操作:
document.getElementById('KeyOfC').pause();
document.getElementById('null').play();
我很乐意得到任何答案,因为我是 JavaScript 新手。
如何在 JavaScript 中创建一个onmousedown
在特定元素中执行的函数:
document.getElementById('KeyOfC').play();
并mouseup
执行此操作:
document.getElementById('KeyOfC').pause();
document.getElementById('null').play();
我很乐意得到任何答案,因为我是 JavaScript 新手。
您可以将这些触发器绑定到元素(在这种情况下,它有一个id
of 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 选择器,比如.ClassName, #your_id, etc.
.