Javascript document.getElementById不工作,即使我的css中有ID
我试着搜索,但没有得到我问题的有效答案。 当我调用Javascript document.getElementById不工作,即使我的css中有ID,javascript,html,css,Javascript,Html,Css,我试着搜索,但没有得到我问题的有效答案。 当我调用document.getElementById(“#”+id)时,我总是得到“null”,并且它会提醒文档未加载 我的Javascript代码 function changeDisplay(id) { alert("#"+id); if (!document.getElementById("#"+id)) { alert("Document Not loaded"); } else {
document.getElementById(“#”+id)
时,我总是得到“null”,并且它会提醒文档未加载
我的Javascript代码
function changeDisplay(id) {
alert("#"+id);
if (!document.getElementById("#"+id)) {
alert("Document Not loaded");
} else {
if (id == "property_photos") {
alert(id);
document.getElementById("#property_photos").style.display = "block";
document.getElementById("#property_details").style.display = "none";
document.getElementById("#property_reviews").style.display = "none";
alert(document.getElementById(id).style.display);
}
if (id == "property_details") {
alert(id);
document.getElementById("#property_photos").style.display = "none";
document.getElementById("#property_details").style.display = "block";
document.getElementById("#property_reviews").style.display = "none";
alert(document.getElementById(id).style.display);
}
if (id == "property_reviews") {
alert(id);
document.getElementById("#property_photos").style.display = "none";
document.getElementById("#property_details").style.display = "none";
document.getElementById("#property_reviews").style.display = "block";
alert(document.getElementById(id).style.display);
}
}
}
HTML
<li>
<a href="#property_photos" onclick="changeDisplay('property_photos')">Photos</a>
</li>
<li>
<a href="#property_details" onclick="changeDisplay('property_details')">Details</a>
</li>
<li>
<a href="#property_reviews" onclick="changeDisplay('property_reviews')">Reviews</a>
</li>
任何帮助都将不胜感激。这是一个简单的问题。我在不同的页面上使用相同的代码,但在我正在使用的页面上没有。我甚至在定义函数之前调用了.css文件,但错误仍然存在
document.getElementById()
不需要英镑(#)符号。document.getElementById()
不需要英镑(#)符号。在使用JavaScript时,不使用
符号来引用元素的ID
你的密码
alert("#"+id);
会是
alert(id);
只有在使用CSS或jQuery时,才需要使用数字符号 在使用JavaScript时,不要使用
#
符号来引用元素的ID
你的密码
alert("#"+id);
会是
alert(id);
只有在使用CSS或jQuery时,才需要使用数字符号 您不需要英镑符号
#
。也许您已经经常使用jQuery了?我知道我以前也犯过同样的错误。你不需要英镑符号。也许您已经经常使用jQuery了?我知道我以前也犯过同样的错误。