gpt4 book ai didi

javascript - 遍历数组并检索值

转载 作者:行者123 更新时间:2023-11-29 20:00:22 25 4
gpt4 key购买 nike

我试图循环一个数组,然后循环一个列表。

我遇到的问题是每个 <li>正在追加整个数组,但我需要做的是将数组的索引 (0) 添加到第一个 li,将索引 (1) 添加到第二个 li,依此类推。

代码:

// Create our test array.
var arrValues = [ "one", "two", "three" ];

// Loop over each value in the array.
$.each(arrValues,function( intIndex, objValue ){
$("#list span").each(function() {
$(this).append(objValue);
});
});

当前输出:

<ul id="list">
<li>Content 1 here<span>onetwothree</span></li>
<li>Content 2 here<span>onetwothree</span></li>
<li>Content 3 here<span>onetwothree</span></li>
</ul>

要求的输出:

<ul id="list">
<li>Content 1 here<span>one</span></li>
<li>Content 2 here<span>two</span></li>
<li>Content 3 here<span>three</span></li>
</ul>

感谢任何帮助:)

最佳答案

只需这样做:

var arrValues = [ "one", "two", "three" ];

$("#list span").each(function(index) {
$(this).append(arrValues[index]);
});

关于javascript - 遍历数组并检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14815883/

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