在下面的示例中,我試圖在用戶單擊鏈接時顯示div
,並且在單擊鏈接時也關閉相同的div
。顯示隱藏鏈接
問題出現在中間的代碼中,該代碼檢查單擊鏈接時是否可見div
。現在,點擊相同的鏈接並不會隱藏div
,這正是我所希望的。
我有點卡在這個。這很簡單,但我無法想象如何正確使用它。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Generated by Edit Plus</title>
<link rel="stylesheet" href="http://localhost/site/css/bootstrap.css"
media="screen">
<script type="text/javascript"
src="http://localhost/site/scripts/jQueryCore.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".addUser").click(function(){
if($('.demoForm').is(':visible')){
$('.demoForm').hide();
}
$(this).parent().find(".demoForm").toggle();
});
});
</script>
<style type="text/css">
.toolOptions{
display:none;
}
</style>
</head>
<body>
<div class="container">
<div>
<div class="addUser">Add a new user
<span class="demoForm" style="display:none;">I'm the form div.</span>
</div>
</div>
<div>
<div class="addUser">Add a new user
<span class="demoForm" style="display:none;">I'm the form div.</span>
</div>
</div>
<div>
<div class="addUser">Add a new user
<span class="demoForm" style="display:none;">I'm the form div.</span>
</div>
</div>
</div>
</body>
</html>
所有誰回答 例如,'if'有沒有刪除任何已有的div可見。我首先查找任何現有的可見div,然後關閉它,然後在它的父級附近打開一個新的div。事情就是這樣。 – jmenezes