JS无法正常工作

奎克莱夫

我有一个HTML页面,带有两个按钮:一个带有按钮onclick="reShuffle('c'),另一个带有onclick="reShuffle('bw')然后,我有这个Javascript:

function reShuffle(set) {
    if (set = "bw") {
        console.log("Shuffling b&w pictures...");
        var blackAndWhite = shuffle(resize([], 20)); // Second arg is # of images in folder
        go(blackAndWhite, "bw");
    }
    if (set = "c") {
        console.log("Shuffling color pictures...");
        var color = shuffle(resize([],  0)); // Second arg is # of images in folder
        go(color, "c");
    }
    function resize(array, size) {
        for (i = 1; i < size + 1; i++){array.push(i)}
        return array;
    }
    function shuffle(array) {
        var i = array.length,
            j = 0,
            temp;
        while (i--) {
            j = Math.floor(Math.random() * (i+1));
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
        console.log("Array shuffled: " + String(array));
        console.log("Length: " + array.length);
        return array;
    }
    function go(listName, shortname) {
        for (i = 1; i < 16; i++) { // (i = 1; i < # of <img>; i++)
            var index = listName[i - 1];
            console.log( i + ": " + index + " = " + listName[i - 1]);
            document.getElementById("img" + i + shortname).src="imgs/" + shortname + "/" + index + ".jpg";
        }
    }
}

reShuffle("bw");
reShuffle("c");

问题是,无论我做什么,reShuffle("bw")或者reShuffle("c")从按钮或控制台进行操作,它都“重新洗牌”。

JS的作用是从目录中获取15张随机图像,并将它们分配给15个<img>标签。因此,它对我的​​黑白图像部分bw以及对颜色部分都这样做c

姆里

正如评论者所说,您需要

if (set == 'bw') {}

if (set == 'c') {}

否则,赋值始终返回true,并执行if测试

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章