Play sound with javascript

前端 未结 3 1150
闹比i
闹比i 2021-01-13 21:43

I want to create a table with string and button(that play sound) in each row and every button play a different sound.

i want to do it with this method :



        
相关标签:
3条回答
  • 2021-01-13 21:59

    You could use a javascript framework to make your code easier. Take a look at the Scriptaculous page:

    http://madrobby.github.com/scriptaculous/sound/

    To my knowledge 'jQuery' is not so well appointed for sound, however, I guess that there are plugins for that.

    Then you could put your sound files as links or 'rel' links, have observers on the buttons and play them with quite clean HTML and a little scripting.

    0 讨论(0)
  • 2021-01-13 22:10

    You can achieve it as follow:

    refer blog How to Play a Sound on Click or on MouseOver

    <script language="javascript" type="text/javascript">
     function playSound(soundfile) {
     document.getElementById("dummy").innerHTML=
     "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
     }
     </script>
    

    In web page

    <span id="dummy"></span>
    <a href="#" onclick="playSound('URL to soundfile');">Click here to hear a sound</a>
    
     <p onmouseover="playSound('URL to soundfile');">Mouse over this text to hear a sound</p>
    
    0 讨论(0)
  • 2021-01-13 22:13

    NOTE: This is not HTML5. You could get the source with thissound.src. Then you can add it to the DOM in whichever location you would like to put it at.

    0 讨论(0)
提交回复
热议问题