我见过类似的问题-但不能解决我的问题!
我的页面上有音频,当一个结束时,我希望下一个开始,但我什至无法触发结束...
我将代码缩减为:
function DaisyChainAudio() {
$().on('ended', 'audio','' ,function () {
alert('done');
});
}
这是从我的页面/代码中调用的(并执行该操作,设置一个断点表明这一点)。
据我了解,这应该在文档级别设置处理程序,因此任何“音频”标签(即使是动态添加)中的任何“结束”事件也应被捕获并向我显示警报...
但是它永远不会触发。
编辑
到目前为止,从ÇağatayGürtürk的建议中得到了一些借鉴...
function DaisyChainAudio() {
$(function () {
$('audio').on('ended', function (e) {
$(e.target).load();
var next = $(e.target).nextAll('audio');
if (!next.length) next = $(e.target).parent().nextAll().find('audio');
if (!next.length) next = $(e.target).parent().parent().nextAll().find('audio');
if (next.length) $(next[0]).trigger('play');
});
});
}
我仍然想在文档级别设置它,所以我不必担心在添加动态元素时添加它...
尝试这个:
$('audio').on('ended', function (e) {
alert('done');
var endedTag=e.target; //this gives the ended audio, so you can find the next one and play it.
});
请注意,在动态创建新音频时,应分配事件。一个快速而肮脏的解决方案是:
function bindEvents(){
$('audio').off('ended').on('ended', function (e) {
alert('done');
var endedTag=e.target; //this gives the ended audio, so you can find the next one and play it.
});
}
并在创建/删除音频元素时运行bindEvents。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句