


I made a random play, and then when I wanted to loop through the asynchronously returned song data, the original effect was to click the left and right buttons to play the previous and next songs, but the result was that clicking the left button directly played the first song. One song, and when the right button is clicked, the last song is played directly. Can anyone help give me a solution? The code is as follows
<code> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href="css/bootstrap.css"> <style> body{ background:deepskyblue; } .container{ margin-top:150px; } a{ color: #000; } span{ font-size: 2.5em; } .music{ border:1px #ddd dashed; } .info li{ color: red; } .list{ border: 1px dashed #ddd; } .time-bar{ width: 0; height: 5px; background: yellow; } </style> <div class="container"> <div class="row"> <div class="col-sm-4 col-sm-offset-4 col-xs-8"> <div class="logo"><img class="img-responsive lazy" src="/static/imghwm/default1.png" data-src="img/bg.jpg" alt=""></div> <div class="music text-center"> <audio id="audio" src="res/TakeMeToYourHeart.mp3" loop="loop" autoplay></audio> <ul class="list-inline list-unstyled info"> <li> <li> </ul> <ul class="list-unstyled list-inline control"> <li><a href="#"><span class="glyphicon glyphicon-backward"></span></a></li> <li><a href="#"><span class="glyphicon glyphicon-play"></span></a></li> <li><a href="#"><span class="glyphicon glyphicon-forward"></span></a></li> </ul> <div class="time-bar"></div> </div> </div> <div class="col-xs-4"> <ul class="list-unstyled list text-center"> <h1 id="歌单">歌单</h1> </ul> </div> </div> </div> <script src="js/jquery-1.8.3.min.js"></script> <script src="js/jquery-ui.js"></script> <script> //加载页面的播放器动画 // setTimeout(function(){ // $('.row').toggle('pulsate'); // console.log('aa'); // },1000); document.getElementById("audio").setAttribute('loop','loop');//循环歌曲 //异步请求数据库歌曲数据 $(document).ready(function(){ $.getJSON('musicList.php',function(data){ console.log(data); var playList=data; doResponse(playList); }) } ); //测试数据 // var playList=[{'player':'薛之谦','music':'来日方长'}, // {'player':'薛之谦','music':'一半.mp3'}, // {"player":'邓紫棋','music':'喜欢你.mp3'}]; function doResponse(playList) { for (var i = 0; i < playList.length; i++) { $('.list-unstyled.list').append('<li>' + playList[i].player + playList[i].music + '') }; var str = $('#audio')[0].getAttribute('src').split('res/'); console.log(str); $('.info li:first-child')[0].innerHTML = str[1].split('.mp3')[0].split('.'); $('.music ul li a').click(function (e) { e.preventDefault(); var index = $(this).parent().index(); var x=parseInt(Math.random()*(playList.length-1)); console.log(x); if (index == 0) { $('audio').attr('src', 'res/' + playList[x].music); $('.info li:first-child')[0].innerHTML = playList[x].player; $('.info li:last-child')[0].innerHTML = playList[x].music.split('.mp3')[0].split('.'); console.log(index); console.log(i); } else if (index == 1) { if ($('#audio')[0].paused) { $('#audio')[0].play(); $('.control li a span:eq(1)')[0].className = 'glyphicon glyphicon-play' } else { $('#audio')[0].pause(); $('.control li a span:eq(1)')[0].className = 'glyphicon glyphicon-stop' } //console.log(index); } else if(index==2) { $('audio').attr('src', 'res/' + playList[x].music); $('.info li:first-child')[0].innerHTML = playList[x].player; $('.info li:last-child')[0].innerHTML = playList[x].music.split('.mp3')[0].split('.'); console.log(index); } //console.log(parseFloat($('#audio')[0].duration)) }); } </script> </code>
Reply content:
I made a random play, and then when I wanted to loop through the asynchronously returned song data, the original effect was to click the left and right buttons to play the previous and next songs, but the result was that clicking the left button directly played the first song. One song, and when the right button is clicked, the last song is played directly. Can anyone help give me a solution? The code is as follows
<code> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href="css/bootstrap.css"> <style> body{ background:deepskyblue; } .container{ margin-top:150px; } a{ color: #000; } span{ font-size: 2.5em; } .music{ border:1px #ddd dashed; } .info li{ color: red; } .list{ border: 1px dashed #ddd; } .time-bar{ width: 0; height: 5px; background: yellow; } </style> <div class="container"> <div class="row"> <div class="col-sm-4 col-sm-offset-4 col-xs-8"> <div class="logo"><img class="img-responsive lazy" src="/static/imghwm/default1.png" data-src="img/bg.jpg" alt=""></div> <div class="music text-center"> <audio id="audio" src="res/TakeMeToYourHeart.mp3" loop="loop" autoplay></audio> <ul class="list-inline list-unstyled info"> <li> <li> </ul> <ul class="list-unstyled list-inline control"> <li><a href="#"><span class="glyphicon glyphicon-backward"></span></a></li> <li><a href="#"><span class="glyphicon glyphicon-play"></span></a></li> <li><a href="#"><span class="glyphicon glyphicon-forward"></span></a></li> </ul> <div class="time-bar"></div> </div> </div> <div class="col-xs-4"> <ul class="list-unstyled list text-center"> <h1 id="歌单">歌单</h1> </ul> </div> </div> </div> <script src="js/jquery-1.8.3.min.js"></script> <script src="js/jquery-ui.js"></script> <script> //加载页面的播放器动画 // setTimeout(function(){ // $('.row').toggle('pulsate'); // console.log('aa'); // },1000); document.getElementById("audio").setAttribute('loop','loop');//循环歌曲 //异步请求数据库歌曲数据 $(document).ready(function(){ $.getJSON('musicList.php',function(data){ console.log(data); var playList=data; doResponse(playList); }) } ); //测试数据 // var playList=[{'player':'薛之谦','music':'来日方长'}, // {'player':'薛之谦','music':'一半.mp3'}, // {"player":'邓紫棋','music':'喜欢你.mp3'}]; function doResponse(playList) { for (var i = 0; i < playList.length; i++) { $('.list-unstyled.list').append('<li>' + playList[i].player + playList[i].music + '') }; var str = $('#audio')[0].getAttribute('src').split('res/'); console.log(str); $('.info li:first-child')[0].innerHTML = str[1].split('.mp3')[0].split('.'); $('.music ul li a').click(function (e) { e.preventDefault(); var index = $(this).parent().index(); var x=parseInt(Math.random()*(playList.length-1)); console.log(x); if (index == 0) { $('audio').attr('src', 'res/' + playList[x].music); $('.info li:first-child')[0].innerHTML = playList[x].player; $('.info li:last-child')[0].innerHTML = playList[x].music.split('.mp3')[0].split('.'); console.log(index); console.log(i); } else if (index == 1) { if ($('#audio')[0].paused) { $('#audio')[0].play(); $('.control li a span:eq(1)')[0].className = 'glyphicon glyphicon-play' } else { $('#audio')[0].pause(); $('.control li a span:eq(1)')[0].className = 'glyphicon glyphicon-stop' } //console.log(index); } else if(index==2) { $('audio').attr('src', 'res/' + playList[x].music); $('.info li:first-child')[0].innerHTML = playList[x].player; $('.info li:last-child')[0].innerHTML = playList[x].music.split('.mp3')[0].split('.'); console.log(index); } //console.log(parseFloat($('#audio')[0].duration)) }); } </script> </code>

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.