1

我在 html 中注册了声音剪辑:

<audio class="aaa" id="sss"><source src="url/sound.wav"/></audio>
<script type="text/javascript">var sss = document.getElementById("sss"); sss.volume='0.1';</script>

可以在特定 div 上使用 mouseenter 事件播放此声音:

$('#divid').mouseenter(function () {
    $(sss.play());
});

如何使用音频类而不是 id 来实现这一点?

编辑:已解决

4

1 回答 1

1

.play()是 HTMLAudioElement 对象而不是 jQuery 对象的方法,jQuery 包装器在这里不做任何事情,因为您将.play()方法的重新调整值传递给它,您可以使用 jQuery 选择元素,获取 DOM 元素对象) 从集合中调用.play()方法:

$('audio.aaa').get(0).play(); // works for the first matched element in the collection

如果有多个元素,您可以使用以下方法遍历集合.each()

$('audio.aaa').each(function() {
   // `this` keyword here refers the HTMLAudioElements
   this.foo();
});
于 2013-09-25T09:28:12.500 回答