用Javascript顯示隱藏層

 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Ajax的Hello頁面</title>
<link href="css/hello.css" rel="stylesheet" type="text/css" /> 

<script language="javascript">
 function ShowHide(button)
 {
  if(button.value=="隱藏")
  {
   button.value="顯示";
   document.getElementById('good').style.display = "none";   //隱藏
  }
  else
  {
   button.value="隱藏";
   document.getElementById('good').style.display = "block";   //顯示
  }
 }
</script>
</head>

<body>
<div id="good">我要顯示的文字</div>
<form action="" method="post">
 <input type="button" value="隱藏"  name="myButton" οnclick="javascript:ShowHide(this)"/>
</form>
</body>
</html>

將以上代碼複製到一個新建的HTML頁面中,即可實現“我要顯示的文字”這句話的隱藏和顯示。

關鍵就是下面兩行代碼:

document.getElementById('good').style.display = "none";   //隱藏

document.getElementById('good').style.display = "block";   //顯示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章