添加事件监听器在JavaScript中的音频HTML5标签 [英] Adding event listener to audio HTML5 tag in javascript
问题描述
你好我创建在Javascript中一个新的音频标签元素,这是code:
Hi I'm creating a new audio tag element in Javascript this is the code:
var audio = document.createElement("audio");
audio.setAttribute("id","myid");
audio.setAttribute("autoplay","autoplay");
document.body.appendChild(audio);
追加到身体之前,我想将一个onended事件处理程序,我想是这样的:
before appending it to the body, I'd like to place an onended event handler, I tried something like this:
audio.onended = foo;
其中foo是一样的东西:函数foo(){警报('霍拉')}
where foo is something like: function foo(){alert('Hola')}
和
audio.setAttribute("onended","foo()");
在这两种情况下,它没有工作。
在第一种情况下,音频标签附加无任何onended事件处理程序;
而在第二种情况下音频代码被附加时,onended事件是上的属性,但它不闪光。
in both case it didn't work. In the first case the audio tag is appended without any onended event handler; while in the second case the audio tag is appended, the onended event is on the attributes but it does not fire.
是否有人有什么想法?
先谢谢了。
-z -
推荐答案
尝试:
audio.addEventListener('ended', foo);
这是添加事件侦听器的正确标准方法。
This is the correct and standard method to add event listeners.
这篇关于添加事件监听器在JavaScript中的音频HTML5标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!