HTML5 音频标签 + Jquery:切换活动按钮
<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;
document.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>
我还尝试使用 $(this).toggle 代替 getElementById('playButton'),但活动/非活动图像不会切换。当我没有切换语句时,自定义按钮可用于启动/暂停/停止音频。添加切换后,控件将不再起作用,但会显示初始的非活动控件图像。我还意识到,当切换一个按钮时,我需要一个单独的函数来重置其他按钮。
<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;
document.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该尝试“$(”#playButton”).toggle(..);”。
您需要有一个 JQuery 对象...
请使用此方法:
您还可以看到 JQuery-ui 及其“按钮”方法!
You should try "$("#playButton").toggle(..);".
You need to have a JQuery object...
Use this method, instead:
You can see also JQuery-ui and its"button" method!