我正在使用此jquery显示/隐藏插件http://papermashup.com/jquery-show-hide-plugin/
我打算在我的页面上点击任意链接关闭之前的div ,但是当我点击新链接时,我之前的div保持打开状态。
我的HTML
<div id="slidingDiv_2" class"toggleDiv"><!-- Conteúdo do menu 1 -->
<div class="opmenu">
<table>
<tr>
<td>
<ul>
<li><a href="#!">CENTROS DE ACTIVIDADES NOS TEMPOS LIVRES</a></li>
<li><a href="#!">SERVIÇO DE ATENDIMENTO E ACOMPANHAMENTO SOCIAL</a></li>
<li><a href="#!">CENTRO COMUNITÁRIO</a></li>
<li><a href="#!">APARTAMENTO PARA A AUTONOMIA DE VIDA</a></li>
</ul>
</td>
</tr>
</table>
</div>
</div>
我的链接:
<li class="items_menu"><a href="#" class="show_hide" rel="#slidingDiv">A INSTITUIÇÃO</a></li>
我的js:
(function ($) {
$.fn.showHide = function (options) {
//default vars for the plugin
var defaults = {
speed: 1000,
easing: 'easeInQuart',
changeText: 0,
showText: 'Mostrar',
hideText: 'Ocultar'
};
var options = $.extend(defaults, options);
$(this).click(function () {
$('.toggleDiv').slideUp(options.speed, options.easing);
// this var stores which button you've clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
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==1){
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
}
});
return false;
});
};
})(jQuery);
答案 0 :(得分:1)
点击任意链接时关闭div
$('a').on('click', function(){
$('Selectorfordiv').slideUp(); // or slideToggle or css({'display':'none'})
});
当div可见以添加一个Class时,可以在单击任何链接时将此目标定位为关闭。
PS。对于show / hide不使用插件,自己编写行会花费更多资源。
答案 1 :(得分:1)
不需要任何插件,如果你也不想使用jquery ui手风琴。
点击此处DEMO http://jsfiddle.net/yeyene/HmwN8/2/
rel
标记中添加a
标记以及您要打开的DIV ID。$(document).ready(function(){
$('a.toggle').on('click', function(){
$('.content').slideUp('2000', "easeOutBounce");
if($('div#'+$(this).attr('rel')).css('display')=='block')
$('div#'+$(this).attr('rel')).slideUp('2000', "easeOutBounce");
else
$('div#'+$(this).attr('rel')).slideDown('2000', "easeOutBounce");
});
});
<a class="toggle" rel="div_1">View 1</a>
<a class="toggle" rel="div_2">View 2</a>
<a class="toggle" rel="div_3">View 3</a>
<div class="content" id="div_1">( 1 ) Lorem Ipsum ...</div>
<div class="content" id="div_2">( 2 ) Lorem Ipsum ...</div>
<div class="content" id="div_3">( 3 ) Lorem Ipsum ...</div>