我真的不知道如何解释我的问题,所以我在下面粘贴了我的代码。我基本上有三个以数字结尾的变量,我想在引用循环索引的循环内调用每个变量。
textLine1 = "This is line 1";
textLine2 = "This is line 2";
textLine3 = "This is line 3";
function text(i) {
for (i = 1; i < 4; i++) {
$('.line__' + i).html(textLine(i) would go here);
}
}
text();
最佳答案
有什么原因不能将它们声明为数组吗?
var textLines = ["This is Line 1", "This is..."];
function text() {
for (var i = 0; i < textLines.length; i++) {
$('.line__' + i + 1).html(textLine[i]);
}
}
关于javascript - 是否可以在引用索引的循环中调用 jQuery 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35096883/