简体   繁体   English

如何<a><button>在JS中点击</button>或</a>发出声音

[英]How to make sound when a <a> or <button> is clicked in JS

How can I make a sound ("MySound.mp3") hidden in the background when a <a> element or <button> element with the id "click_sound" is clicked in JavaScript, is it possible? 当在JavaScript中单击带有id“click_sound”的<a>元素或<button>元素时,如何在背景中隐藏声音(“MySound.mp3”),是否可能?

Remember I want to make a sound in the background if an <a> or <button> are clicked! 请记住,如果单击<a><button> ,我想在后台发出声音!

The Code: 编码:

<html>
  <head></head>
  <body>
    <a id="click_sound">Click me</a>
  <button id="click_sound">Click me</button>
  </body>
</html>

The sound file name: 声音文件名:
MySound.mp3 MySound.mp3

The easiest way to do what you are asking is to play the audio using javascript, when the button or a (or really any) tag is clicked. 做你所要求的最简单的方法就是打使用javascript音频上,当buttona (或任何真正的)标签被点击。 There are a few ways you could do this: 有几种方法可以做到这一点:

You could run the javascript in an onmouseup element: 您可以在onmouseup元素中运行javascript:

<html>
  <head></head>
  <body>
    <a id="click_sound" onmouseup="new Audio('path_to_audio_file.mp3').play()">Click me</a>
    <button id="click_sound" onmouseup="new Audio('path_to_audio_file.mp3').play()">Click me</button>
  </body>
</html>

or better yet, not run the audio constructor every time: 或者更好的是,不是每次都运行音频构造函数:

<html>
  <head></head>
  <body>
    <a id="click_sound" onmouseup="audio.play()">Click me</a>
    <button id="click_sound" onmouseup="audio.play()">Click me</button>

      <script>const audio = new Audio('path_to_audio_file.mp3')</script>
  </body>
</html>

If you want to use an external script (either because you have other javascript, or you just prefer it that way, which I personally do) simply put it on the window object: 如果你想使用一个外部脚本(因为你有其他的javascript,或者你只是喜欢它,我个人这样做)只需将它放在window对象上:

<html>
  <head></head>
  <body>
    <a id="click_sound" onmouseup="window.audio.play()">Click me</a>
    <button id="click_sound" onmouseup="window.audio.play()">Click me</button>

      <script src='./path_to_file/somefile.js'></script>
  </body>
</html>

somefile.js: somefile.js:

  window.audio = new Audio('./path_to_audio/sound.mp3')

Please let me know if you need any more information about any of the above. 如果您需要有关上述任何内容的更多信息,请与我们联系。

Note: You could also use: 注意:您还可以使用:

document.getElementById('click_sound').addEventListener('mouseup', () =>  new Audio('path_to_audio_file.mp3').play())

To achieve the same thing. 实现同样的目标。

For more information refer to this and this or look up "javascript 'Audio' constructor" and "onmouseup event". 欲了解更多信息,请参阅或查找“JavaScript的‘音频’构造”和“onmouseup事件”。

我怎么做<div>单击按钮时部分不可见(JS)?</div><div id="text_translate"><p> 如何使用 JS 隐藏&lt;div&gt;部分和</p><pre>&lt;div class="alertB1"&gt; &lt;div class="sticky"&gt; &lt;h1&gt; This site is still being built&lt;/h1&gt; &lt;p&gt; Please remember this site is still being built. Click &lt;a href="form1.html"&gt;here to report a bug &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.alertB1 { width: 100%; height: 125px; background: lightgreen; } &lt;/style&gt;</pre><p> 所以我希望包含一个按钮:</p><pre> &lt;span id="a33"&gt;&lt;button&gt;&amp;times;&lt;/button&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 如何让&lt;span&gt;隐藏&lt;div&gt;标签? 谢谢,环形游戏</p></div> - How do i make a <div> section invisible when a button is clicked (JS)?

暂无
暂无

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

相关问题 单击键/按钮时如何播放每种声音? - How can I play each sound when a key/button is clicked? 如何在小吃 expo 中单击停止按钮时停止声音? - How to stop a sound when the stop button is clicked in snack expo? 如何使按钮仅在通过基本JS单击另一个按钮时出现 - how to make a button appear only when another button is clicked through basic JS 单击按钮后,Chrome扩展程序无法播放声音吗? - Chrome Extension not playing sound when button is clicked? 单击“暂停”按钮时,fadeOut()会发出声音 - fadeOut() sound when Pause button is clicked 单击按钮时播放声音 html - Play sound when button clicked html 在javascript中单击按钮时添加声音 - Adding sound when button is clicked in javascript 单击 HTML 按钮时尝试播放声音 - Trying to play a sound when HTML button is clicked 单击按钮时如何运行JS代码 - how to run JS code when button is clicked 我怎么做<div>单击按钮时部分不可见(JS)?</div><div id="text_translate"><p> 如何使用 JS 隐藏&lt;div&gt;部分和</p><pre>&lt;div class="alertB1"&gt; &lt;div class="sticky"&gt; &lt;h1&gt; This site is still being built&lt;/h1&gt; &lt;p&gt; Please remember this site is still being built. Click &lt;a href="form1.html"&gt;here to report a bug &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.alertB1 { width: 100%; height: 125px; background: lightgreen; } &lt;/style&gt;</pre><p> 所以我希望包含一个按钮:</p><pre> &lt;span id="a33"&gt;&lt;button&gt;&amp;times;&lt;/button&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 如何让&lt;span&gt;隐藏&lt;div&gt;标签? 谢谢,环形游戏</p></div> - How do i make a <div> section invisible when a button is clicked (JS)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM