2013-09-25 88 views
1

我已经注册在HTML声音片段:Seelcting音频元素

<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> 

这声音可利用MouseEnter事件在某些DIV播放:

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

这怎么可能用音频类代替id实现?

编辑:解决

回答

1

.play()是HTMLAudioElement对象不是jQuery对象的方法,jQuery的包装并没有在这里做任何事情,因为你正在过.play()方法,它的重新调整值,您可以选择使用jQuery元素(一个或多个),从收集得到的DOM元素对象(S)和调用其.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(); 
}); 
+0

由于某种原因,此工作仅适用于Firefox,不适用于Opera和Chrome。 – weaponx

+0

@weaponx每个浏览器都支持不同的音频格式,https://developer.mozilla.org/en-US/docs/HTML/Supported_media_formats – undefined

+0

我的不好。 “问题”取决于缓存。您的代码在所有3个浏览器上都能顺利运行。谢谢您的帮助。 – weaponx