我试图将此分开"因为"循环创建5个不同的DIV元素

时间:2017-11-16 03:53:07

标签: javascript jquery html css

谢谢你路过。我真的是一个新手,而且我正在努力(通过采取一些代码,希望这并没有错)5个不同的玩家,我使用了以下代码:{{ 3}}

我试图将以下循环分成5个不同的div(5个不同的玩家,而不是#audio-players,每个人都有一个新的div):

function createAudioPlayers() {
    for (f in files) {
        var playerString = "<div id=\"audioplayer-" + f + "\" class=\"audioplayer\"><button id=\"playbutton-" + f + "\" class=\"play playbutton\"></button><div id=\"timeline-" + f + "\" class=\"timeline\"><div id=\"playhead-" + f + "\" class=\"playhead\"></div></div></div>";
        $("#audio-players").append(playerString);
    }
}

以下是从中提取文件的代码:

var files = ["interlude.mp3", // 0
            "chooseyourweapon.mp3", // 1
            "interlude.mp3", // 2
            "scriptures.mp3",
       "scriptures.mp3"// 3
            ];

我不确定如何继续,我还在学习。谢谢。

2 个答案:

答案 0 :(得分:0)

使用

bower update
npm update 

取代

for (f of files) {

有关详情:https://bitsofco.de/for-in-vs-for-of/

答案 1 :(得分:0)

似乎你只需要循环数组。使用forEach以便于理解

&#13;
&#13;
function createAudioPlayers() {
  files.forEach(function(f) {
    var playerString = "<div id=\"audioplayer-" + f + "\" class=\"audioplayer\"><button id=\"playbutton-" + f + "\" class=\"play playbutton\">" + f + "</button><div id=\"timeline-" + f + "\" class=\"timeline\"><div id=\"playhead-" + f + "\" class=\"playhead\"></div></div></div>";
    $("#audio-players").append(playerString);
  })
}


var files = ["interlude.mp3", // 0
  "chooseyourweapon.mp3", // 1
  "interlude.mp3", // 2
  "scriptures.mp3",
  "scriptures.mp3" // 3
];
createAudioPlayers()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="audio-players"></div>
&#13;
&#13;
&#13;