对于给定的 html:
<div id="parent">
<div class="child">x</div>
<div class="child">y</div>
<div class="child">z</div>
</div>
如何从数组映射其子级的文本?
这是我的尝试:
var myArray = ['a', 'b', 'c'];
for (var i = -1; i < myArray.length; i++) {
$('#parent .child').each(function () {
$(this).text(myArray[i]);
});
}
我得到:c,c,c
如何获得a,b,c
?
最佳答案
您不需要外部 for
循环:
var myArray = ['a', 'b', 'c'];
$('#parent .child').each(function (index) {
$(this).text(myArray[index]);
});
关于javascript - 在 for 循环中使用 jQuery.each 将数组映射到元素文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42404143/