2014-06-21 73 views
1

我試過搜索,但沒有得到我的問題的有效答案。 當我撥打document.getElementById("#"+id)時,我總是會收到「空」,並且它會提醒Document not loadeddocument.getElementById不工作,即使我的ID存在我的CSS

我的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

#property_photos { 
    display:none; 
} 
#property_details { 
    display:block; 
} 
#property_reviews { 
    display:none; 
} 

任何幫助將不勝感激。它是一個簡單的問題。我得到了相同的代碼在不同的頁面上工作,但不在頁面m上工作。我在定義函數之前甚至調用了.css文件,但錯誤仍然存​​在。

+0

你不需要英鎊符號'#'。也許你一直在使用jQuery?我知道我以前犯過同樣的錯誤。 –

回答

4

document.getElementById()確實不是需要英鎊(#)符號。

1

在使用JavaScript時,不要使用#符號來引用元素的ID。

您的

alert("#"+id); 

代碼將是

alert(id); 

你需要#數字符號,用CSS或jQuery的工作,只有當。

相關問題