如何在按鈕單擊時顯示div,否則將其隱藏起來?僅當單擊按鈕時才顯示div
我已經作秀按鈕:
<script>
$("#show").click(function(){
$("#content").show();
});
});
</script>
<div class="content">Content of div</p>
<button id="show">show</button>
如何在按鈕單擊時顯示div,否則將其隱藏起來?僅當單擊按鈕時才顯示div
我已經作秀按鈕:
<script>
$("#show").click(function(){
$("#content").show();
});
});
</script>
<div class="content">Content of div</p>
<button id="show">show</button>
我建議你使用toogle()
代替show()
和hide()
。
而且還把你的代碼在ready()
。
如果您不想從頭開始顯示,請使用display : none
或visibility: 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>
你div有一流的內容,而你正在使用的ID選擇,而當前腳本有語法錯誤,也有在年底 –
額外括號@UmaKanth我的壞 – Jules