我正在尝试在 div 之间切换,以便在单击标签时显示 div。单击另一个 a 标签时,该 div 将替换显示的 div。
这就是我所做的。
HTML:
<a href="#" rel="#slidingDiv">a</a><br>
<a href="#" rel="#slidingDiv_2">b</a><br>
<a href="#" rel="#slidingDiv_3">c</a><br>
<a href="#" rel="#slidingDiv_4">d</a><br>
<div id="slidingDiv">a</div>
<div id="slidingDiv_2">a</div>
<div id="slidingDiv_3">a</div>
<div id="slidingDiv_4">a</div>
查询:
function ($) {
$.fn.showHide = function (options) {
//default vars for the plugin
var defaults = {
speed: 1000,
easing: '',
changeText: 0,
showText: 'Show',
hideText: 'Hide'
};
var options = $.extend(defaults, options);
$(this).click(function () {
var toggleDiv;
var $divA = $('#slidingDiv'),
$divB = $('#slidingDiv_2'),
$divC = $('#slidingDiv_3'),
$divD = $('#slidingDiv_4'),
$divE = $('#slidingDiv_5'),
$divF = $('#slidingDiv_6'),
$divG = $('#slidingDiv_7'),
$divH = $('#slidingDiv_8'),
$divI = $('#slidingDiv_9');
if( $divA.is( ':visible' ) ){
$divA.hide();
}
if( $divB.is( ':visible' ) ){
$divB.hide();
}
if( $divC.is( ':visible' ) ){
$divC.hide();
}
if( $divD.is( ':visible' ) ){
$divD.hide();
}
if( $divE.is( ':visible' ) ){
$divE.hide();
}
if( $divF.is( ':visible' ) ){
$divF.hide();
}
if( $divG.is( ':visible' ) ){
$divG.hide();
}
if( $divH.is( ':visible' ) ){
$divH.hide();
}
if( $divI.is( ':visible' ) ){
$divI.hide();
}
// this reads the rel attribute of the button to determine which div id to toggle
toggleDiv = $(this).attr('rel');
$('.toggleDiv').slideUp(options.speed, options.easing);
// this var stores which button you've clicked
var toggleClick = $(this);
var toggleDiv = $(this).attr('rel');
// here we toggle show/hide the correct div at the right speed and using which easing effect
$(toggleDiv).slideToggle(options.speed, options.easing, function() {
// this only fires once the animation is completed
// if(options.changeText==0){
//$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
//}
});
return false;
});
};
})(jQuery);
这目前有效,但我知道这可以做得更好,而不是使用 if 语句。
谢谢