在下面的共享stackblitz示例中,我为音频元素包装绑定了mousedown和click事件,该事件未被触发.我需要执行自己的操作,并执行音频元素单击.

无法找到此 case 的任何解决方案,对此有任何 idea 都将不胜感激.

<span id="test">
<audio controls>
  <source src="https://www.w3schools.com/html/horse.mp3" type="audio/mpeg">
</audio>
<br>
</span>
<script>
document.querySelector('#test').addEventListener('click', function() { console.log ('click called')});
document.querySelector('#test').addEventListener('mousedown', function() { console.log ('mousedown called')});

样本:https://stackblitz.com/edit/9gw8e5-qujbbj?file=index.html,index.js

任何关于这方面的建议,都可以实现这一行为.在MDN中也找不到关于此的任何建议(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio).

推荐答案

try 了以下在figure HTML元素中包装的方法,这是audio元素之上的替代解决方案instead of wrapping a separate element.希望将音频元素包装在内联/块 node 中的用户.

```css
figure {
    display: block;
    outline: none;
    position: relative;
    margin: 0;
    padding: 0;
}

figure:after {
    position: absolute;
    content: "";
    z-index: 1;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    cursor: default;
    display: block;
    background: transparent;
}

```html
<span id="test">
  <audio controls>
    <source src="https://www.w3schools.com/html/horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
  </audio>
</span>

样本:https://stackblitz.com/edit/9gw8e5-dswrh3?file=index.html,index.js

上面的示例使鼠标向下并单击操作适用于音频元素.

希望它能帮助别人..!

Javascript相关问答推荐

文件阅读器未读取一个文件

我不明白这个react 组件有什么问题

如何使用Paged.js仅渲染特定页面

有什么(最佳)方法可以从模块中获取脚本模块的多姆元素吗?

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

单击更新页面的按钮后,页面刷新;测试/断言超时,有两个标题,但没有一个标题

我们如何从一个行动中分派行动

屏幕右侧屏障上的产卵点""

使用LocaleCompare()进行排序时,首先使用大写字母

编剧如何获得一个div内的所有链接,然后判断每个链接是否都会得到200?

本地库中的chartjs-4.4.2和chartjs-plugin-注解

连接到游戏的玩家不会在浏览器在线游戏中呈现

为什么我的自定义元素没有被垃圾回收?

使用Document.Evaluate() Select 一个包含撇号的HTML元素

打字脚本中方括号符号属性访问和拾取实用程序的区别

OnClick更改Json数组JSX中的图像源

未捕获语法错误:Hello World中的令牌无效或意外

图表4-堆叠线和条形图之间的填充区域

MongoDB中的嵌套搜索

谷歌饼图3D切片