我的 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/