質問

Can I have an image folder path and an url path in two variables (E.g var path = css/image/ and var urlpath = /pic/funny/) and append them before every image and url respectively so that I don't have to repeat the pathes for every image in 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];
                }

            }

I have tried something like mainImgs[i].src = path.picO[randIndex[i]].src; but I can't get the syntax right.

役に立ちましたか?

解決

Use + to combine variables, not .

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

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

Or as then by your example:

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

You also need to quote your variables:

var urlpath = "/pic/funny/";
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top