所以毕竟我操纵了大量的 JS 代码来提防你的回调地狱。毕竟你应该为元素使用类。很难查看您的代码。但结果如下:
jsfiddle (http://jsfiddle.net/RKVY7/7/)
HTML
<a class="togglelink" data-target="div1">togglelink1</a>
<a class="togglelink" data-target="div2">togglelink2</a>
<a class="togglelink" data-target="div3">togglelink3</a>
<div class="container">
<div class="toggleddivs" id="div1">CONTENT_DIV1 <a class="toggle" data-target="div1">CLOSE</a></div>
<div class="toggleddivs" id="div2">CONTENT_DIV2 <a class="toggle" data-target="div2">CLOSE</a></div>
<div class="toggleddivs" id="div3">CONTENT_DIV3 <a class="toggle" data-target="div3">CLOSE</a></div>
</div>
CSS(用于 .container)
.container {
display: inline-block;
padding: 0;
margin : 0;
}
JS
jQuery(document).ready(function(){
// INT
// the height of the highest one
var highestcomefareper = 0;
// get highest comefareper so it will not scroll up.
var _gethighestcomefareper = function () {
var _height = jQuery(this).height();
if(highestcomefareper<_height) {
highestcomefareper = _height;
}
};
// It's not a "real toggle". Just close all and slide down the one
var _togglecomefareper = function(){
var _target = jQuery(this).data('target');
jQuery(".comefareper").hide();
jQuery("#"+_target).slideDown();
};
// close the nearest element with the class "comefareper". So don't use it in these boxes again
var _closecomefareper = function () {
jQuery(this).closest('.comefareper').slideUp();
};
// Get highest comefareper
jQuery(".comefareper").each(_gethighestcomefareper);
// Assign height to the container. (Prevents page from up-scrolling on close)
jQuery(".container").css("min-height", highestcomefareper + "px");
// Hide All
jQuery(".comefareper").hide();
// Assign functions to the buttons
jQuery('.toggle').click(_togglecomefareper);
jQuery(".close").click(_closecomefareper);
});