<!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="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" onclick="javascript:ShowHide(this)"/>
</form>
</body>
</html>
將以上代碼複製到一個建立的HTML頁面中,即可實現“我要顯示的文字”這句話的隱藏和顯示。
關鍵就是下面兩行代碼:
document.getElementById('good').style.display = "none"; //隱藏
document.getElementById('good').style.display = "block"; //顯示
下面是啟動並執行效果:
點“隱藏”之後的效果: