toggle
[API Ref]会起作用,但它作用于display
CSS 属性,而不是visibility
. 只需使用display
:
<div id="div1" onclick="toggleDivs();">
div1 content
</div>
<div id="div2" onclick="toggleDivs();" style="display: none;">
div2 content
</div>
和脚本:
function toggleDivs() {
$('#div1, #div2').toggle();
}
这是一个工作示例。
附录:
与以前的解决方案相比,我不太关心这个解决方案,但是如果根据下面的 OP 评论,您想使用 完成此任务z-index
,您可以这样做:
HTML:
<div id="div1" class="cycle">
div1 content
</div>
<div id="div2" class="cycle">
div2 content
</div>
CSS:
.cycle {
position: absolute; /* The important thing is that the element
is taken out of the document flow */
background: #fff;
width: 100px;
height: 20px;
border: solid 1px #000;
}
JavaScript:
$(function() {
var cycleClick = function(e) {
var $cycle = $('.cycle');
$cycle.each(function() {
var $this = $(this);
var newZIndex = ($this.css('z-index') + 1) % $cycle.length;
$this.css('z-index', newZIndex);
});
return false;
};
$('.cycle').click(cycleClick).each(function(idx) {
$(this).css('z-index', idx);
});
});