Javascript 未捕获的TypeError:document.getElementById不是函数
在我的随机脚本中,我得到一个错误:uncaughttypeerror:document.getElementById不是一个函数。我研究了堆栈溢出错误的一些其他问题,并试图修复它。我在函数之前声明了变量,因为变量每次都会更改,所以我将它设置为另一个变量。 我不知道为什么这个代码是一个问题,任何帮助是感激的。 HTML: 沙文Javascript 未捕获的TypeError:document.getElementById不是函数,javascript,html,typeerror,Javascript,Html,Typeerror,在我的随机脚本中,我得到一个错误:uncaughttypeerror:document.getElementById不是一个函数。我研究了堆栈溢出错误的一些其他问题,并试图修复它。我在函数之前声明了变量,因为变量每次都会更改,所以我将它设置为另一个变量。 我不知道为什么这个代码是一个问题,任何帮助是感激的。 HTML: 沙文 var al; var rl; var bd; function gp(){ players = document.getElementById("input").
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!");
}
}
你的问题是这一行:
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!";
}
}
因为您正在为document.getElementById分配字符串值!通过学习JavaScript.IMO的基础知识,即使这个问题缺乏研究,你也不应该回答得很差。谢谢你的反对票和意见。一旦被指出,答案应该是显而易见的。在我看来像是眼睛疲劳综合症。我觉得这个问题不需要完整的解释。我仍然很惊讶这个问题是开放的,我猜美国假日已经减缓了接近投票的速度。当用户花时间回答正确的问题时,你应该放弃投票。@TyrionGraphiste不完整的答案可能是正确的,但仍然不完整。不管怎么说,已经取消了否决票,因为它得到了改进。
function BlankDisplay(){
if(counter == 1){
document.getElementById('someId').innerHTML = "Click The Button Above To See Your Job!";
}
}