简体   繁体   English

选择图像并通过使用html和javascript单击按钮来播放音频

[英]select image and and play an audio by clicking the button using html and javascript

<audio id="audio1" src="a.wav"></audio>
<audio id="audio2" src="b.wav"></audio>

<img id="img1" src="a.png" alt=""  onselect="select()"  />
 <img id="img2" src="a.png" alt=""  onselect="select()"  />  

<input type="button"  onclick="play()">

i have multiple images in my code a and every image has its respective audio what i want to do is i want to select the image and on clicking the button i want to play the audio against that particular image .all this i want to do using javascript and html . 我在代码a中有多个图像,并且每个图像都有其各自的音频,我要选择的是图像,然后单击按钮,我要针对该特定图像播放音频。 javascript和html。 so can any one help me javascript part? 所以有人可以帮助我javascript部分吗?

Just add this and it should work: 只需添加它,它应该可以工作:

<img id="img1" src="a.png" alt=""  onclick="select(this.id)"  />
 <img id="img2" src="a.png" alt=""  onclick="select(this.id)"  />  

<script>
  function select(num){
       var x = num.match('/\d+/');
       var audio = document.getElementById("audio"+x);
       audio.play();
                 }
</script>

You can do something like this using data attributes: http://jsfiddle.net/bostdgvp/ 您可以使用数据属性执行以下操作: http : //jsfiddle.net/bostdgvp/

JS: JS:

var selectedImage;

function select(image) {
    var images = document.querySelectorAll('img');
    var index = 0, length = images.length;
    for (; index < length; index++) {
        images[index].classList.remove('selected');
    }
    selectedImage = image.id;
    image.classList.add('selected');
}

function playAudio() {
    if (!selectedImage) {
        alert('no image selected')
    }
    var image = document.getElementById(selectedImage);
    var audioId = image.getAttribute('data-audio')
    var audioElement = document.getElementById(audioId);
    audioElement.play();
}

HTML: HTML:

<audio id="audio1" src="@Url.Content(item.Letter_Record)"></audio>
<audio id="audio2" src="@Url.Content(item.Letter_Record)"></audio>
<img id="img1" data-audio="audio1" src="http://placehold.it/350x150" alt="" onclick="select(this)" />
<img id="img2" data-audio="audio2" src="http://placehold.it/200x100" alt="" onclick="select(this)" />
<input type="button" value="play" onclick="playAudio()">

CSS: CSS:

.selected {
    border: 1px solid red;
}

Here is a jsfiddle that should put you in the right direction: 这是一个jsfiddle,它可以帮助您朝正确的方向发展:

http://jsfiddle.net/knopch/31oasnee/ http://jsfiddle.net/knopch/31oasnee/

<img id="img1" src="a.png" alt=""  onclick="select(this.id)"  />
<img id="img2" src="a.png" alt=""  onclick="select(this.id)"  />  

<script>
    function select(id){
      console.log(id);
      //play(id);
    }
</script>

This allows you to click the image, and its id will be printed in the console (option+cmd+j on chrome+mac). 这使您可以单击图像,并且其ID将被打印在控制台中(在chrome + mac上为option + cmd + j)。 So if you have the id of the object being clicked (or selected), the select() functions really just need to pass on this id to the function that handles playback. 因此,如果您具有要单击(或选中)的对象的ID,则select()函数实际上只需要将此ID传递给处理播放的函数。 If you have an audio file mapped to each image, you can select the audio file for playback based on the image id. 如果您有一个音频文件映射到每个图像,则可以根据图像ID选择要播放的音频文件。

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

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