问题
i have a vertical menu bar which contains sub menu. There are 9-10 menus and each menu contains 3 sub menus.
what i need if menu 1 is open and someone click on menu 3 menu 1 should close & menu 3 will open
$('#nav li a').click(function(){
var sds = document.getElementById("dum");
if(sds == null){
;
}
var sdss = document.getElementById("dumdiv");
if(sdss == null){
}
if(sdss != null){
var s = $(this).attr('id');
var imgid=$("#"+s+" img").attr('id');
var imgsrc=$("#"+imgid+"").attr('src');
if(imgsrc=="images/insert.GIF")
{
$("#"+imgid+"").attr('src','images/remove.GIF');
$(this).next().slideDown(400);
$("#"+s+"").css("background-color","#142878");
}
else
{
$("#"+imgid+"").attr('src','images/insert.GIF');
$(this).next().slideUp(400);
$("#"+s+"").css("background-color","#2d539a");
}
}
});
here's the fiddle
回答1:
Try this:
$('.count').slideUp(400); //<----add this line
$(this).next().slideDown(400);
Demo
Updates:
try this:
if (imgsrc == "images/insert.GIF") {
$("#" + imgid + "").attr('src', 'images/remove.GIF');
$('.count').slideUp(400);
$(this).next().slideDown(400);
$("#" + s + "").css("background-color", "#142878");
} else {
$("#" + imgid + "").attr('src', 'images/insert.GIF');
$('.count').slideUp(400); //<--------------------add here
$(this).next().slideDown(400); //<---------------and here too.
$("#" + s + "").css("background-color", "#2d539a");
}
回答2:
Use
$('a').next().slideUp(400);
If you click on any menu, this will close the current menu and opens the new one
Here is the updated Fiddle
回答3:
Add this to the beginning your code:
$('#nav li a').click(function(){
$(this).closest('li').siblings('li').find('.count').slideUp();
// Rest of the code here
Updated Demo
来源:https://stackoverflow.com/questions/21253306/sliding-multiple-elements-on-click