Javascript 未捕获的类型错误:document.getElementById 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30448439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:11:07  来源:igfitidea点击:

Uncaught TypeError: document.getElementById is not a function

javascripthtmltypeerror

提问by Coler234

In my randomizing script I'm getting the error: Uncaught TypeError: document.getElementById is not a function. I looked at some other issues with this error on stack overflow and tried to fix it. I declared the variable before the function, and because a variable changes every time, I set it to another variable. I have no idea why this code is an issue, any help is appreciated. HTML:

在我的随机化脚本中,我收到错误:Uncaught TypeError: document.getElementById is not a function。我在堆栈溢出时查看了此错误的其他一些问题,并尝试修复它。我在函数之前声明了变量,因为每次都会有一个变量发生变化,所以我将它设置为另一个变量。我不知道为什么这个代码是一个问题,任何帮助表示赞赏。HTML:

<div id="GetPlayers">
    <input maxlength="1" id="input"> <button id="gp" onclick="gp()">Start!</button>
    <br/>
    <br/>
    <button id="DJ" onclick="DJ();">Display Your Job!</button>
    <br/>
    <span id="DS">1) Input Amount Of Players. 2)Click 'Display Your Job!'</span>
</div>

Javscript

脚本

var al;
var rl;
var bd;
function gp(){
    players = document.getElementById("input").value;
    if(isNaN(players)){
        alert(players.toUpperCase() + "'s Players? Please Fix"); 
        return;
    }
    if(players === " "){
        alert("Please Define How Many People Are Playing!");
        return;
    }
    if(players === ""){
        alert("Please Define How Many People Are Playing!");
        return;
    }
    if(players < 4){
        alert("Sorry, You Need Atleast 4 Players To Play!");
        return;
    }
    SA(players)
}
function SA(players){
    var positions = ["Murderer", "Judge", "Innocent", "Innocent"]; //Pre-set positions
    if(players == 5){
        positions.push("Co-Judge");
    }else if(players == 6){
        positions.push("Innocent", "Co-Judge"); 
    }else if(players == 7){
        positions.push("Murderer-2!", "Innocent", "Co-Judge");
    }
    Randomize(players, positions)
}
function shuffle(o){
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}
function Randomize(players, positions){
    rl = shuffle(positions);
    al = positions.length;
    confirm("You Have: " + al + " Players, Correct?");
    alert(al + ". " + rl);
}
var counter;
var rl;
var c;
var p;
function DJ(){
    counter = 0;
    var bd = 0;
    for(var c = 0; c < al + 1; c++){
        if(counter == 0){
            p = c;
            document.getElementById("DS").innerHTML=(rl[p]);
            document.getElementById("DJ").innerHTML=("Click To Clear!");
            counter = 1;
            BlankDisplay()
        }
    }
}
function BlankDisplay(){
    if(counter == 1){
        document.getElementById=("Click The Button Above To See Your Job!");   
    }
}

回答by

In this function:

在这个函数中:

function BlankDisplay(){
    if(counter == 1){
        document.getElementById=("Click The Button Above To See Your Job!");   
    }
}

You're redefining document.getElementById, to a string.

您正在将document.getElementById,重新定义为字符串。

So, whenever you call document.getElementByIdagain, after that, you're trying to execute a string, hence why you get not a function. So I guess you want to change that to:

因此,无论何时document.getElementById再次调用,之后,您都在尝试执行一个字符串,因此为什么会得到not a function. 所以我想你想把它改成:

function BlankDisplay(){
    if(counter == 1){
        document.getElementById('someId').innerHTML = "Click The Button Above To See Your Job!";   
    }
}

回答by epascarello

Well your problem is this line:

那么你的问题是这一行:

document.getElementById=("Click The Button Above To See Your Job!");

It is not selecting an element and setting the text like you do in a bunch of other places, you are setting it to a string.

它不是像在其他地方一样选择一个元素并设置文本,而是将它设置为一个字符串。