Adding event listener to audio HTML5 tag in javascript(在javascript中将事件侦听器添加到音频HTML5标签)
问题描述
您好,我正在 Javascript 中创建一个新的音频标签元素,代码如下:
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 类似于:function foo(){alert('Hola')}
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标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在javascript中将事件侦听器添加到音频HTML5标签


基础教程推荐
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01