开发者

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 ids (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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜