Javascript 极简播放()和暂停()音频解决方案
我在另一个问题中发现了一个小代码片段,用jquery play()和pause()播放mp3: 还有.find()和.each(),但到目前为止没有任何效果。我已经设置了一个带有两个音频链接的音频链接,其中只会播放第一个音频文件。(fiddle链接到一个外部脚本,客户端在他的站点上使用,那里只加载了jquery 1.4.3,但我想这是可能的。我只是不想使用音频播放器插件,我的目标是一个最低限度的解决方案。)Javascript 极简播放()和暂停()音频解决方案,javascript,jquery,audio,Javascript,Jquery,Audio,我在另一个问题中发现了一个小代码片段,用jquery play()和pause()播放mp3: 还有.find()和.each(),但到目前为止没有任何效果。我已经设置了一个带有两个音频链接的音频链接,其中只会播放第一个音频文件。(fiddle链接到一个外部脚本,客户端在他的站点上使用,那里只加载了jquery 1.4.3,但我想这是可能的。我只是不想使用音频播放器插件,我的目标是一个最低限度的解决方案。) 任何帮助都将不胜感激 您可以更新脚本,为每个容器创建一个音频标记: $(document
任何帮助都将不胜感激 您可以更新脚本,为每个容器创建一个音频标记:
$(document).ready(function () {
// For each container div
$(".container").each(function() {
// Create the HTML5 <audio> tag
var audioElement = document.createElement('audio');
// Find the play/pause buttons
var $play = $(this).find(".play");
var $pause = $(this).find(".pause");
// Load the source from the play button
var source = $play.attr('rel');
audioElement.setAttribute('src', source);
$.get();
// Play the sound when loaded
audioElement.addEventListener("load", function () {
audioElement.play();
}, true);
// When the user clicks on the play button, play the audio
$play.click(function () {
audioElement.play();
});
// When the user clicks on the pause button, pause it
$pause.click(function () {
audioElement.pause();
});
});
});
$(文档).ready(函数(){
//对于每个容器分区
$(“.container”).each(函数(){
//创建HTML5@Alani没问题,如果它适合你,请标记为答案:)
var source = $(this).attr('rel');
$(document).ready(function () {
// For each container div
$(".container").each(function() {
// Create the HTML5 <audio> tag
var audioElement = document.createElement('audio');
// Find the play/pause buttons
var $play = $(this).find(".play");
var $pause = $(this).find(".pause");
// Load the source from the play button
var source = $play.attr('rel');
audioElement.setAttribute('src', source);
$.get();
// Play the sound when loaded
audioElement.addEventListener("load", function () {
audioElement.play();
}, true);
// When the user clicks on the play button, play the audio
$play.click(function () {
audioElement.play();
});
// When the user clicks on the pause button, pause it
$pause.click(function () {
audioElement.pause();
});
});
});