gpt4 book ai didi

javascript - 如何使用变量作为图像文件夹路径

转载 作者:行者123 更新时间:2023-11-29 17:10:33 24 4
gpt4 key购买 nike

我可以在两个变量中有一个图像文件夹路径和一个 url 路径吗(例如 var path = css/image/var urlpath =/pic/funny/)并将它们分别附加在每个图像和 url 之前,这样我就不必为 var picData?

中的每个图像重复路径
Array.prototype.shuffle = function() {
var s = [];
while (this.length) s.push(this.splice(Math.random() * this.length, 1));
while (s.length) this.push(s.pop());
return this;
}

var picData = [
['img1','url_1'],
['img2','url_2'],
['img3','url_3'],
['img4','url_4'],
['img5','url_5'],
['img6','url_6'],
['img7','url_7']];

picO = new Array();
randIndex = new Array(); //array of random indexes
for(i=0; i < picData.length; i++){
picO[i] = new Image();
picO[i].src = picData[i][0];
picO[i].alt = picData[i][1];
randIndex.push(i);
}
randIndex.shuffle();
window.onload=function(){
var mainImgs = document.getElementById('carouselh').getElementsByTagName('img');


for(i=0; i < mainImgs.length; i++){
mainImgs[i].src = picO[randIndex[i]].src; //assign a random image
mainImgs[i].parentNode.href = picData[randIndex[i]][1];
mainImgs[i].alt = picData[randIndex[i]][1];
}

}

我尝试过类似 mainImgs[i].src = path.picO[randIndex[i]].src; 的方法,但语法不正确。

最佳答案

使用+来组合变量,而不是

var a = "Some";
var b = "day";
var c = " we are gone.";

alert(a + " " + b + c);

或者按照你的例子:

mainImgs[i].src = path + picO[randIndex[i]].src;

您还需要引用您的变量:

var urlpath = "/pic/funny/";

关于javascript - 如何使用变量作为图像文件夹路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956403/

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