我知道这个主题有很多话题,但是这些话题都没有解决我的问题。

我有一个javascript代码,其中有几个函数,大约一个小时后,我终于发现在特殊函数下定义的所有函数均不起作用,并且将出现错误“ ReferenceError:找不到变量:functionName”,并且在该特殊功能之上定义的所有其他功能都可以正常工作。
我的问题是我无法找出导致此问题的特殊功能出了什么问题……有人可以帮助我吗?

这是特殊功能:

function shift(btn) {
if (!shiftPressed) {
    document.getElementById("keyShift1").style.background = "rgb(180,50,0)";
    document.getElementById("keyShift2").style.background = "rgb(180,50,0)";
    for (var i = 65; i <= 90; i++) {
        var id = "key" + String.fromCharCode(i);
        document.getElementById(id).innerHTML = document.getElementById(id).value.toUpperCase();
    }
    document.getElementById("key~").innerHTML = "`";
    shiftPressed = !shiftPressed;

} else {
    document.getElementById("keyShift1").style.background = "black";
    document.getElementById("keyShift2").style.background = "black";

    document.getElementById("key~").innerHTML = "~";
    if (!capsPressed) {
        for (var i = 65; i <= 90; i++) {
            var id = "key" + String.fromCharCode(i);
            document.getElementById(id).innerHTML = document.getElementById(id).value.toLowerCase();
        }
    }
    shiftPressed = !shiftPressed;
}

最佳答案

您错过了函数结尾的花括号

它应该是

function shift(btn) {
    if (!shiftPressed) {
        document.getElementById("keyShift1").style.background = "rgb(180,50,0)";
        document.getElementById("keyShift2").style.background = "rgb(180,50,0)";

        for (var i = 65; i <= 90; i++) {
            var id = "key" + String.fromCharCode(i);
            document.getElementById(id).innerHTML = document.getElementById(id).value.toUpperCase();
        }

        document.getElementById("key~").innerHTML = "`";
        shiftPressed = !shiftPressed;
    } else {
        document.getElementById("keyShift1").style.background = "black";
        document.getElementById("keyShift2").style.background = "black";
        document.getElementById("key~").innerHTML = "~";

        if (!capsPressed) {
            for (var i = 65; i <= 90; i++) {
                var id = "key" + String.fromCharCode(i);
                document.getElementById(id).innerHTML = document.getElementById(id).value.toLowerCase();
            }
        }

        shiftPressed = !shiftPressed;
    }
}

关于javascript - ReferenceError:找不到变量:functionName,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36644565/

10-12 22:30