【发布时间】:2014-09-19 10:04:42
【问题描述】:
我正在研究一些在线 mp3 播放列表解决方案,但遇到了一些奇怪的问题。我想在所有存在或将来存在的音频元素上绑定“结束”事件。策略是在现有元素播放结束后追加新的音频元素。代码如下所示:
<!DOCTYPE HTML>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
</head>
<body>
<div id='audio_content'>
<audio class='playable' controls >
<source rel='1' src="/files/1.mp3" type="audio/mpeg"/>
</audio>
</div>
<script type="text/javascript">
$(".playable").on('ended',function(){
var id = $(this).find('source').attr('rel');
$("#audio_content").append("<audio class='playable' controls ><source rel='"+(parseInt(id)+1)+"' src='/files/"+(parseInt(id)+1)+".mp3' type='audio/mpeg'/></audio>");
$("[rel="+(parseInt(id)+1)+"]").closest('audio')[0].play()
});
</script>
</body>
</html>
实际上事件只绑定在第一个元素上,事件中附加的第二个元素没有绑定
【问题讨论】:
标签: javascript jquery html html5-audio