gpt4 book ai didi

javascript - 如何将数组传递给 jquery jaudio 中的函数

转载 作者:行者123 更新时间:2023-11-28 05:47:36 26 4
gpt4 key购买 nike

通过使用此函数,我尝试将多个对象添加到临时数组中。

var temp = [];

function addSong(id) {
var button = $('#' + id + ''); // Button that triggered the modal
var file = button.data('file'); // Extract info from data-* attributes
var thumb = button.data('thumb'); // Extract info from data-* attributes
var trackName = button.data('track_name'); // Extract info from data-* attributes
var trackArtist = button.data('track_artist'); // Extract info from data-* attributes
var trackAlbum = button.data('track_album'); // Extract info from data-* attributes

temp += "{file:'" + file + "',thumb:'" + thumb + "',trackName:'" +
trackName + "',trackArtist:'" + trackArtist + "',trackAlbum:'" + trackAlbum + "'},";

$("#player").jAudio({
playlist: []
});
}

最佳答案

您需要传递对象数组而不是字符串。就像下面的示例

$("#player").jAudio({
playlist: [{
file: "resources/tracks/01.mp3",
thumb: "resources/thumbs/01.jpg",
trackName: "Track 1",
trackArtist: "Artist 1",
trackAlbum: "Album",
}, {
file: "resources/tracks/02.mp3",
thumb: "resources/thumbs/02.jpg",
trackName: "Track 2",
trackArtist: "Artist 2",
trackAlbum: "Album",
}]
});

因此将您的临时变量更改为类似

var temp =  {
file: file,
thumb: thumb,
trackName: trackName,
trackArtist: trackArtist,
trackAlbum: trackAlbum
};

然后在数组中使用它

  $("#player").jAudio({
playlist: [temp]
});

根据更新的问题,您可以将您的js函数修改为这样的

 var temp = [];

function addSong(id) {

var button = $('#' + id + ''); // Button that triggered the modal
var file = button.data('file'); // Extract info from data-* attributes
var thumb = button.data('thumb'); // Extract info from data-* attributes
var trackName = button.data('track_name'); // Extract info from data-* attributes
var trackArtist = button.data('track_artist'); // Extract info from data-* attributes
var trackAlbum = button.data('track_album'); // Extract info from data-* attributes

var tempObj = {
file: file,
thumb: thumb,
trackName: trackName,
trackArtist: trackArtist,
trackAlbum: trackAlbum
};

temp.push(tempObj);

$("#player").jAudio({
playlist: temp
});
}

关于javascript - 如何将数组传递给 jquery jaudio 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38365211/

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