开发者

HTML5 Audio tag + Jquery: Toggling the active button

开发者 https://www.devze.com 2023-02-25 15:34 出处:网络
<div id=\"audio-player\"> <div> <img id=\"playButton\" onclick=\"document.getElementById(\'stars\').play();
<div id="audio-player">

   <div>

      <img id="playButton" 
    onclick="document.getElementById('stars').play(); 
        document.getElementById('playButton').toggle(
            function(){ $(this).attr("src", "playbutton-active.png"); },
            function(){ $(this).attr("src", "playbutton-inactive.png"); } );" 
        src="playbutton-inactive.png">

      <img id="pauseButton" 
    onclick="document.getElementById('stars').pause();
    document.getElementById('pauseButton').toggle(
        function(){ $(this).attr("src", "pausebutton-active.png"); },
        function(){ $(this).attr("src", "pausebutton-inactive.png"); } );"
        src="pausebutton-inactive.png">

      <img id="stopButton" 
     onclick="document.getElementById('stars').pause(); 
     document.getElementById('stars').currentTime = 0;
     documen开发者_Python百科t.getElementById('stopButton').toggle(
        function(){ $(this).attr("src", "stopbutton-active.png"); },
        function(){ $(this).attr("src", "stopbutton-inactive.png"); } );"
        src="stopbutton-inactive.png">

  </div>

  <audio id="stars">
    <source src="stars-in-the-night-sky.mp3">
    <source src="stars-in-the-night-sky.ogg">
  </audio>

I also tried instead of getElementById('playButton'), using $(this).toggle but active/inactive images don't toggle. When I don't have the toggle statements, the custom buttons work to start/pause/stop audio. Adding the toggle, the controls no longer function, though the initial inactive control images show up. I realize also that I would need a separate function to reset the other buttons when one is toggled.


You should try "$("#playButton").toggle(..);". You need to have a JQuery object...

Use this method, instead:

<script type="text/javascript">
  $(document).ready(function(){
    $("#playButton").click(function(){
      $(this).toggle(..);
    });
  });
</script> 

You can see also JQuery-ui and its"button" method!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号