js實現的點選顯示或者隱藏相關內容

螞蟻小編發表於2017-03-16

很多應用中,可能會有這樣的效果,一些內容在預設狀態下是隱藏的,當點選一個按鈕就會將此內容顯示出來,下面就通過程式碼例項簡單介紹一下如何實現此效果,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script type="text/javascript"> 
function $(idvalue){ 
  return document.getElementById(idvalue); 
} 
function showtext(){ 
  if($('text').style.display=='none'){ 
    $('text').style.display=''; 
    $("themore").value="隱藏內容";
  }
  else{ 
    $('text').style.display='none'; 
    $("themore").value="顯示內容";
  } 
} 
window.onload=function(){
  var themore=document.getElementById("themore");
  themore.onclick=function(){
    showtext();
  }
}
</script> 
</head> 
<body> 
<div id="text" style="display:none;">螞蟻部歡迎您</div> 
<input id="themore" type="button" value="檢視資訊" />
</body> 
</html>

以上程式碼實現了我們的要求,點選按鈕可以切換內容的是否存在。

相關文章