从动态生成的音频标签中收听audio.ended
I have a page that generates dynamic audio elements and places them into the DOM. Here is a rough example of what my code is doing:
我有一个生成动态音频元素的页面并将它们放入DOM中。这是我的代码正在做的一个粗略的例子:
...
$("#container").html("<audio src='" + song.url + "'>");
Late in the code, I want to run some code when that audio element is finished playing it's file. But the ended
vent seems to never get fired.
在代码的后面,我想在音频元素播放完文件时运行一些代码。但最终的发泄似乎永远不会被解雇。
$("#container").on('ended', function() {
console.log("this never gets called");
});
How can I catch the ended
event (or any other event) from a dynamically generated audio tag?
如何从动态生成的音频标签中捕获结束事件(或任何其他事件)?
edit: If I place the declaration of the event handler right after creating the audio element it works.
编辑:如果我在创建音频元素之后立即放置事件处理程序的声明,它就可以工作。
$("#container").html("<audio src='" + song.url + "'>");
$("#container audio").on('ended', function() {
console.log("this does get called");
});
But this solution doesn't completely solve my problem because this on
call needs to go in a Chrome Extension, which can't be recalled every time a new audio element is created.
但是这个解决方案并没有完全解决我的问题,因为这个随叫随到需要进入Chrome扩展程序,每次创建新的音频元素时都无法调用。
1 个解决方案
#1
Add the audio tag into the on call to allow event delegation.
将音频标签添加到通话中以允许事件委派。
$("#container").on('ended', 'audio', function() {
console.log("this never gets called");
});
http://api.jquery.com/on/
更多相关文章
- Javascript XML DOM将属性设置为特定元素
- 获取JavaScript数组元素的最大长度
- 获取html DOM元素内容的边框
- 是否有办法阻止contenteditable的插入符号出现在IE10的元素上?
- 获取拆分字符串数组的最后一个元素
- CSS3(jQUery?)当它悬停时隐藏元素“a”,这样就可以显示元素“b”
- 如何在点击时动态加载/播放/暂停多源HTML5音频?
- 停止鼠标用javascript双击某些元素
- 在量角器中检索子元素的数组