onmousedown play sound
How do I make a function in JavaScript that onmousedown
in a specific elemtent executes this:
document.getElementById('KeyOfC').play();
and on mouseup
executes this:
document.getElementById('KeyOfC').pause();
document开发者_如何转开发.getElementById('null').play();
I'd be happy for any answers, as I'm new to JavaScript.
You can just bind those triggers to the element (in this case it has an 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();
}
If you want to specify multiple id
s (from your comment), you'd need a JavaScript library which does this. I'd recommend jQuery:
$('#foo, #bar, #foobar').mousedown(function()
{
$('#KeyOfC').play();
});
$('#foo, #bar, #foobar').mouseup(function()
{
$('#KeyOfC').pause();
$('#null').play();
});
If you've done CSS, you can use CSS rules as jQuery Selectors, like .ClassName, #your_id, etc.
.
精彩评论