仅当单击按钮时才显示div
问题描述:
如何在按钮单击时显示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