简体   繁体   English

如何有效地将声音添加到 HTML 或 CSS 中的按钮?

[英]How do I effectively add sound to a button in HTML or CSS?

I need to know this for a school project, I need the button to play a sound onclick.我需要知道这个对于一个学校项目,我需要按钮来播放声音 onclick。 I tried using <button> and an <audio> tag, but they don't seem to work.我尝试使用<button><audio>标签,但它们似乎不起作用。 CSS would be an alternative. CSS 将是一个替代方案。 The only thing that's worked is:唯一有效的是:

<form action="https://bestringtoness.com/sounds/mp3/darth-vader-noooo.mp3">
  <input type="submit" value="Yes" />
</form>

(actual code I'm using) (我正在使用的实际代码)

But it's very inefficient, and I need to go back and press 'Run' over and over just to click different options.但它的效率非常低,我需要返回 go 并一遍又一遍地按“运行”以单击不同的选项。

Use an audio tag like this:使用这样的audio标签:

 <audio id="a" src="https://bestringtoness.com/sounds/mp3/darth-vader-noooo.mp3" autoplay="false" ></audio> <input type="submit" onclick="play()" value="Yes" /> <script> function play() { var sound = document.getElementById("a"); sound.play(); } </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM