2015-07-11 78 views
1

如何在按鈕單擊時顯示div,否則將其隱藏起來?僅當單擊按鈕時才顯示div

我已經作秀按鈕:

<script> 
$("#show").click(function(){ 
     $("#content").show(); 
    }); 
}); 
</script> 

<div class="content">Content of div</p> 

<button id="show">show</button> 
+0

你div有一流的內容,而你正在使用的ID選擇,而當前腳本有語法錯誤,也有在年底 –

+0

額外括號@UmaKanth我的壞 – Jules

回答

0

您可以添加visibility屬性像可視性一個div:「隱藏」

+0

但股利不應該可見。只有那個按鈕被點擊。 – Tim

3

我建議你使用toogle()代替show()hide()

而且還把你的代碼在ready()

如果您不想從頭開始顯示,請使用display : nonevisibility: hidden;屬性。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
 
<script> 
 
$(document).ready(function(){ 
 
    $("button").click(function(){ 
 
     $("div").toggle(); 
 
    }); 
 
}); 
 
</script> 
 
</head> 
 
<body> 
 

 
<div style = "display: none;">This is a div.</div> 
 

 
<button>Toggle between hide() and show()</button> 
 

 
</body> 
 
</html>

+0

哦,謝謝!但是當按鈕被點擊時,div應該是可見的。不直接在頁面的連接處。 – Tim

+0

我想我需要一個js事件來調用我的頁面。 – Tim

+0

@Tim我不認爲這是必需的,一個小的'CSS'調整是綽綽有餘 –