2014-09-29 98 views
0

我試圖讓我們的員工會面部分只顯示一次文本「會見我們的員工」點擊。當點擊「我們的部分員工」時,我已經使用獲取它的代碼隱藏了該部分,但無法弄清楚如何將其翻轉到頁面加載時已隱藏的位置,然後在「遇到我們的員工「被點擊。如何在單擊鏈接之前隱藏元素?

腳本:

<script type="text/javascript"> 
function hideshow(which){ 
if (!document.getElementById) 
return 
if (which.style.display="block") 
which.style.display="none" 
else 
which.style.display="block" 
} 
</script> 

HTML:

<div class="content3"> 
<div class="title2"> 
<h1><center><a href="javascript:hideshow(document.getElementById('staff'))">Meet Some 
of Our Staff</a></center></h1> 
</div> 

<div id="staff"> 
<center> 
<ul> 

<a href="http://www.instituteforcreativelearners.org/" 
onclick="centeredPopup(this.href,'myWindow','500','300','yes');return false"> 
<li> 
<img src="images/kami.jpg" alt="..."> 
<br /> 
<p><span class="stafftitle">Kami Barron</span> <br /> <span 
class="staffsubtitle">Director of Education</span></p> 
</li> 
</a> 

</ul> 
</center> 
</div> 
</div> 
+0

使用CSS將顯示設置爲無。 – 2014-09-29 21:54:30

+0

供參考:['getElementById()'](http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-getElBId)是**函數**,需要1個參數。 – 2014-09-29 21:57:26

回答

0

檢查

Sample - 格式化HTML/JS

變化

if (which.style.display="block") 

if (which.style.display == "block") 

,如果你不設置display:none在CSS中,然後使用這個

if (!which.style.display || which.style.display == "block") 
0

使用CSS來隱藏元素在頁面加載時。

<div id="staff" style="display: none;"> 

這將隱藏div直到用戶點擊鏈接。

編輯:退房this JSFiddle使用代碼

0

在要被隱藏應用CSS規則display:none什麼工作的例子。然後在Javascript中,

document.getElementsByTagName("a")[0].addEventListener("click",function(e) 
{ 
    document.getElementById("staff").style.display="block"; 
    e.preventDefault() 
});