javascript - 未捕获的 TypeError : document. getElementById 不是函数

标签 javascript html typeerror

在我的随机化脚本中,我收到错误: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>

JavaScript

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!");   
    }
}

最佳答案

在此函数中:

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

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

因此,每当您再次调用 document.getElementById 时,您都会尝试执行一个字符串,因此您会得到不是函数。所以我想你想将其更改为:

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

关于javascript - 未捕获的 TypeError : document. getElementById 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30448439/

相关文章:

html - 文本位于单元格中间,不会向上移动

python-2.7 - 将带有逗号的 Pandas 字符串列更改为 Float

javascript - 无法读取 null 的属性 'offsetHeight'

javascript - Backbone 模型初始化

html - 包裹非 float 元素

javascript - 给定数据破解HMAC sha256摘要私钥需要多长时间

php - 是否可以在 TideSDK 应用程序中运行 CodeIgniter?

javascript - “Violation readystatechange handler took 760ms” 更新 Chrome 后

javascript - React.addons.TestUtils.Simulate.scroll 不工作

javascript - 如何调整大 svg 的大小以适应 div