如何在JavaScript中创建一个函数,让特定元素中的onmousedown执行以下操作:
document.getElementById('KeyOfC').play();在mouseup上执行以下命令:
document.getElementById('KeyOfC').pause();
document.getElementById('null').play();我很乐意得到任何答案,因为我是JavaScript的新手。
发布于 2011-03-24 23:38:13
您可以将这些触发器绑定到元素(在本例中,元素的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(来自你的注释),你需要一个JavaScript库来做这件事。我推荐jQuery
$('#foo, #bar, #foobar').mousedown(function()
{
$('#KeyOfC').play();
});
$('#foo, #bar, #foobar').mouseup(function()
{
$('#KeyOfC').pause();
$('#null').play();
});如果你做过CSS,你可以像.ClassName, #your_id, etc.一样使用CSS规则作为jQuery Selectors。
https://stackoverflow.com/questions/5421625
复制相似问题