简体   繁体   English

如何在使用 1 类/1 ID 的同时使用 JavaScript 播放多个音频声音而不必使用多个函数来播放它们

[英]How can I play multiple audio sounds using JavaScript while using 1 class/1 ID and not having to use multiple functions to play them

I have multiple audio files (5).我有多个音频文件 (5)。 I'm attaching the files to an image so that when they are clicked, they play a specific audio.我将文件附加到图像上,以便在单击它们时播放特定的音频。 However, I have to use 5 different IDs & functions to make them play a unique sound.但是,我必须使用 5 个不同的 ID 和函数来让它们播放独特的声音。

HTML: HTML:

<img src="image1" alt="" class="box rounded shadow-lg cursor-pointer" id="" value="PLAY" onclick="ricekrispy()">
        
<audio id="ricekrispy" src="sound.mp3"></audio>

<img src="image2" alt="" class="box rounded shadow-lg cursor-pointer" id="" value="PLAY" onclick="skittles()">
        
<audio id="skittles" src="sound.mp3"></audio>

<img src="image3" alt="" class="box rounded shadow-lg cursor-pointer" id="" value="PLAY" onclick="Mandms()">
        
<audio id="Mandms" src="audio3.mp3"></audio>

<img src="image4" alt="" class="box rounded shadow-lg cursor-pointer" id="" value="PLAY" onclick="nuggets()">
        
<audio id="nuggets" src="audio4.mp3"></audio>

<img src="image5" alt="" class="box rounded shadow-lg cursor-pointer" id="" value="PLAY" onclick="fries()">
        
<audio id="fries" src="audio5.mp3"></audio>

JavaScript: JavaScript:

function ricekrispy() {
    let audio = document.getElementById('ricekrispy');
    audio.play();
  }

  function skittles() {
    let audio = document.getElementById('skittles');
    audio.play();
  }

  function mandms() {
    let audio = document.getElementById('mandms');
    audio.play();
  }

  function nuggets() {
    let audio = document.getElementById('nuggets');
    audio.play();
  }

  function fries() {
    let audio = document.getElementById('fries');
    audio.play();
  }

I reused the functions and changed the name 1 by 1. I'm looking for a way to use a forEach to grab the element/ID/class without having to put script onto the element我重复使用了这些函数并将名称 1 个更改为 1 个。我正在寻找一种方法来使用forEach来获取元素/ID/类,而不必将脚本放在元素上

(value="play" & onclick="function()")

The OP might consider a more modern and generic DOM scripting approach which frees the OP from most id attributes and from inline-scripting. OP 可能会考虑一种更现代和通用的 DOM 脚本编写方法,该方法将 OP 从大多数id属性和内联脚本中解放出来。

The next following implementation uses...接下来的实现使用...

One would query a node-list of image elements which all feature a custom data-audio-source attribute.人们会查询图像元素的节点列表,这些元素都具有自定义data-audio-source属性。

document.querySelectorAll('img[data-audio-source]');

Such data-* global attributes are good for carrying additional information which relates to the very element that is featuring it.这样的data-*全局属性有利于携带与它的元素相关的附加信息。 Here one would assign the audio-source that is related to its image.这里将分配与其图像相关的音频源。

<img src="/path/to/image" data-audio-source="/path/to/audio" />

One then needs to subscribe forEach node-list's image-element an event-listener with a callback-function which handles eg the image-element's 'click' -event .然后需要为每个节点列表的图像元素forEach一个带有回调函数的事件监听器,该回调函数处理例如图像元素的'click'事件

The handler-function which gets passed as callback to addEventListener expects an event -object as its sole parameter.作为回调传递给addEventListener的处理函数需要一个event对象作为其唯一参数。 The event's currentTarget does refer to the clicked image-element (the very target of the event subscription).事件的currentTarget确实引用了单击的图像元素(事件订阅的目标)。 Since such an element features a custom data-audio-source attribute one can read the attribute-value via the element's related dataset -property...由于这样的元素具有自定义data-audio-source属性,因此可以通过元素的相关dataset -属性读取属性值...

const source = elmImg.dataset.audioSource;

Within the handler one would query the correct audio-player element, pause it, assign the correct media-source/reference to it and start play ing it (again).在处理程序中,将查询正确的音频播放器元素, pause它,为其分配正确的媒体源/引用并开始(再次) play它。

 function handleAutoPlay(evt) { const elmImg = evt.currentTarget; const source = elmImg.dataset.audioSource; const elmPlayer = document.querySelector('#auto-player'); elmPlayer.pause(); elmPlayer.src = source; elmPlayer.play(); } document.querySelectorAll('img[data-audio-source]').forEach(elmImg => elmImg.addEventListener('click', handleAutoPlay) );
 body { margin: 0; } p { margin: 0 0 5px 0; } img[data-audio-source] { height: 70px; cursor: pointer; }
 <p> Featuring royalty-free sounds from <a href="https://soundbible.com/royalty-free-sounds-1.html"> soundbible.comy </a> and free pictures from <a href="https://picsum.photos/images"> Lorem Picsum </a> </p> <img src="https://fastly.picsum.photos/id/364/5000/2917.jpg?hmac=xXeSnI5JaHB8KssawSc9gjgKEorKVXx7T_YgPCf2F-A" data-audio-source="https://soundbible.com/mp3/airplane-landing_daniel_simion.mp3" /> <img src="https://fastly.picsum.photos/id/404/3264/2176.jpg?hmac=3lDeBx5WYEse6sijzGfqsQniZqTpFmfFlDnBC3cXzao" data-audio-source="https://soundbible.com/mp3/heavy-rain-daniel_simon.mp3" /> <img src="https://fastly.picsum.photos/id/111/367/267.jpg?hmac=4RBVF232Jl16jusqD0DurHz2lI4m6I8SVW7J6bSnK0Y" data-audio-source="https://soundbible.com/mp3/old-car-engine_daniel_simion.mp3" /> <img src="https://fastly.picsum.photos/id/419/3456/2304.jpg?hmac=RXPdqWRwlAeofpGH8aDVH7Yz7h2VklC82ppVCx5wnKk" data-audio-source="https://soundbible.com/mp3/service-bell_daniel_simion.mp3" /> <audio id="auto-player" controls></audio>

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

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