Unca无法设置 null 的属性 'innerHTML'
UncaCannot set property 'innerHTML' of null
请帮我看看代码有什么问题?
function runPrompt(Message, promptLocation, color)
{
document.getElementById("promptLocation").innerHTML = Message;
document.getElementById("promptLocation").style.color = color;
}
Name :
<input id= "commandName" onkeyup = "validateName()" type = "text"> <label id = "namePrompt"></label>
如果不发布您的 html,就不清楚您在追求什么。我认为您正在尝试使用 promptLocation 参数作为要更改的元素的 id。参数变量应直接传递给 getElementById,不应在引号中:
<div id="namePrompt"></div>
function runPrompt(Message, promptLocation, color)
{
document.getElementById(promptLocation).innerHTML = Message;
document.getElementById(promptLocation).style.color = color;
}
runPrompt('Test Message', 'namePrompt', 'red');
为了安全起见,我们需要在使用'document.getElementById'时检查元素是否存在。因为我们无法访问 JavaScript.
中 undefined/no 渲染元素的属性
在 JQuery 的情况下,如果 id 不存在,$('#id') 将给出空数组。
请帮我看看代码有什么问题?
function runPrompt(Message, promptLocation, color)
{
document.getElementById("promptLocation").innerHTML = Message;
document.getElementById("promptLocation").style.color = color;
}
Name :
<input id= "commandName" onkeyup = "validateName()" type = "text"> <label id = "namePrompt"></label>
如果不发布您的 html,就不清楚您在追求什么。我认为您正在尝试使用 promptLocation 参数作为要更改的元素的 id。参数变量应直接传递给 getElementById,不应在引号中:
<div id="namePrompt"></div>
function runPrompt(Message, promptLocation, color)
{
document.getElementById(promptLocation).innerHTML = Message;
document.getElementById(promptLocation).style.color = color;
}
runPrompt('Test Message', 'namePrompt', 'red');
为了安全起见,我们需要在使用'document.getElementById'时检查元素是否存在。因为我们无法访问 JavaScript.
中 undefined/no 渲染元素的属性在 JQuery 的情况下,如果 id 不存在,$('#id') 将给出空数组。