html+js實現動態顯示隱藏

通過設置display屬性可以使div隱藏後釋放佔用的頁面空間,如下

  style = "display: none;"

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

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

 

參考實例代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="X-UA-Compatible" content="IE=7"/>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>visibility與display的差別</title>

</head>

<script type="text/javascript">

function $(id){ return id?document.getElementById(id):''; }

</script>

<body>

<div id="s1" style="width:200px; height:200px; background-color:#f00; float:left;">這是一個顯示層,使用display顯示與隱藏!</div>

<div id="s2" style="width:200px; height:200px; background-color:#Aff;float:left;">這是一個顯示層,使用visibility顯示與隱藏!</div>

<br/><br/>

<input type="button" οnclick="$('s1').style.display = 'none';" value="display隱藏層" /><br/>

<input type="button" οnclick="$('s1').style.display = 'block';" value="display顯示層" /><br/>

<input type="button" οnclick="$('s2').style.visibility = 'hidden';" value="visibility隱藏層" /><br/>

<input type="button" οnclick="$('s2').style.visibility = 'visible';" value="visibility顯示層" />

</body>

</html>

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