gpt4 book ai didi

jquery - jQuery播放停止音频文件

转载 作者:行者123 更新时间:2023-12-03 00:30:55 25 4
gpt4 key购买 nike

我有歌曲列表,即时通讯使用的方式是:

<a href="path to song 1">play</a>
<a href="path to song 2">play</a>
...

当链接被点击时,这首歌开始了,我用jquery做到了,就像这样:
$('a').on('click', function() {
var path = $(this).attr('href');
var song = new Audio(path);
song.play();
});

单击链接后如何进行播放/停止切换?

最佳答案

像这样切换play()pause()函数:

var isPlaying = false
$("a").on("click", function(e) {
e.preventDefault();

var path = $(this).attr('href');
var song = new Audio(path);

if (!isPlaying) {
isPlaying = true;
song.play();
$(this).text("Pause")
} else {
isPlaying = false;
song.pause();
$(this).text("Play");
}

console.log(isPlaying);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<a href="path to song 1">play</a>
<a href="path to song 2">play</a>

关于jquery - jQuery播放停止音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48746270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com