javascript - Click 事件不会在 Chrome 中的音频元素上触发

标签 javascript html audio click addeventlistener

我的 DOM 中有一个音频元素。 我执行了以下命令,似乎由于某种原因单击事件没有触发:

$0.addEventListener('click',function(){console.log('click')});

当我尝试添加鼠标悬停处理程序时,它按预期工作:

$0.addEventListener('mouseover',function(){console.log('mouseover')});

在 Firefox 中,单击事件正常工作。 有什么想法吗?

最佳答案

"If the user agent exposes a user interface to the user by displaying controls over the media element, then the user agent should suppress any user interaction events while the user agent is interacting with this interface. (For example, if the user clicks on a video's playback control, mousedown events and so forth would not simultaneously be fired at elements on the page.)" — from The Web Hypertext Application Technology Working Group

它的目的是不起作用,因此“恶意”网站无法捕获用户对媒体控件的点击。所以一般情况下不建议绕过它;但如果你真的必须这样做,你必须遵循一些技巧,比如以编程方式将音频元素包装在具有更高 z 索引的透明元素中(小心 z 索引和透明度)并捕获所述元素的点击。

关于javascript - Click 事件不会在 Chrome 中的音频元素上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44613124/

相关文章:

javascript - 最小化 iPad 浏览器 safari 时仍在播放音频

html - 表格中带有奇怪填充的图像

javascript - 汉堡菜单不起作用

Java - 使用音频剪辑时出现问题

javascript - 如何从异步调用返回响应?

javascript - else 语句不起作用是我在 jquery 上的语法不正确吗?

javascript - 获得对 CSSMediaRule 的可读访问权限

javascript - 在 ng-repeat 中使用 ng-list 分割字符串

html - MVC 中的样式下拉列表

audio - 托管音频文件