gpt4 book ai didi

javascript - 数组不迭代值,只迭代数组名称

转载 作者:行者123 更新时间:2023-11-28 18:54:24 25 4
gpt4 key购买 nike

我知道这是基本的,但是,我正在尝试迭代随机选择的数组的内容。此 JavaScript 返回随机数组的名称,但不列出其内容。任何帮助都会有用。

<script language="javascript" type="text/javascript">
var intros = ['introOne', 'introTwo'];

var opening = intros[Math.floor(Math.random() * intros.length)];

var introOne = [
'<img src="assets/redOne.jpg">',
'<img src="assets/redTwo.jpg">',
'<img src="assets/redThree.jpg>'];

var introTwo = [
'<img src="assets/rblueOne.jpg">',
'<img src="assets/rblueTwo.jpg">',
'<img src="assets/blueThree.jpg>'];


for (var i=0; i<opening.length; i++) {
document.write(opening[i]);


}

</script>

最佳答案

引用数组名称时不要引用它们。

var introOne = [
'<img src="assets/redOne.jpg">',
'<img src="assets/redTwo.jpg">',
'<img src="assets/redThree.jpg">'
];

var introTwo = [
'<img src="assets/rblueOne.jpg">',
'<img src="assets/rblueTwo.jpg">',
'<img src="assets/blueThree.jpg">'
];

var intros = [introOne, introTwo];

var opening = intros[Math.floor(Math.random() * intros.length)];

for (var i = 0; i < opening.length; i++) {
document.write(opening[i]);
}

关于javascript - 数组不迭代值,只迭代数组名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927444/

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