我在创建ASK问题的文档时意识到了这个问题的解决方案...所以我发布了答案,如果没有其他原因,也是为了将来不浪费时间。
<html>
<head>
<style>
#upperDiv {width:200px; height:20px; text-align:center; cursor:pointer; }
#lowerDiv {width:200px; height:20px; background-color:red; border:#206ba4 1px solid;}
</style>
<script language="Javascript" src="javascript/jquery-1.2.6.min.js"></script>
<script type="text/JavaScript">
$(function(){
$('#upperDiv').toggle (
function(){
$("#lowerDiv").hide() ;
},
function(){
$("#lowerDiv").show() ;
}
); // End toggle
}); // End eventlistener
</script>
</head>
<body>
<div id="upperDiv" >Upper div</div>
<div id="lowerDiv" >Lover Div</div>
</body>
</html>
toggle()
在某些情况下相当不稳定,我曾经遇到它会对勾选框(取消勾选)和其他各种问题进行奇怪的操作。我建议只使用点击事件来处理它,这样就少了很多小烦恼。而且这种方式,您不必担心首先 #lowerDiv
处于什么状态:
$('#upperDiv').click(function() {
$('#lowerDiv').animate({
'opacity' : 'toggle',
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/.../jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#pClick").click(function(){
$("#pText").toggle();
$("#pText").text("...");
});
});
</script>
在 CSS 文件中:
#pText {display: none;}