gpt4 book ai didi

javascript - 在 jQuery .each() 中使用循环时遇到问题

转载 作者:行者123 更新时间:2023-11-27 23:19:30 25 4
gpt4 key购买 nike

我有两个数组 statememlist正如你所看到的,我正在加载 memlist 的元素至#list ul。这是通过使用.each()来实现的。函数,但我仍然需要加载 state 的元素作为 <li> 的类定义如下所示:

memlist.append($( '<li class="'+ state[i] +'">' + value + '</li>' ));

您能告诉我如何在 jQuery 中执行此操作吗?

var memlist = $( "#list" );

var states = ['one','two','three'];
var members = [ "John", "Steve", "Ben", "Damon", "Ian" ];
$.each(members,function( index, value ){
memlist.append($( "<li>" + value + "</li>" ));
});

谢谢

最佳答案

当达到状态长度时,它会重新开始

var memlist = $( "#list" );

var states = ['one','two','three'];
var members = [ "John", "Steve", "Ben", "Damon", "Ian", "test"];
var counter = 0;
$.each(members,function( index, value ){
//console.log(counter);
memlist.append($( "<li class="+states[counter]+">" + value + "</li>" ));
counter++;
if(counter == states.length){
counter=0;
}

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

关于javascript - 在 jQuery .each() 中使用循环时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502860/

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