I have a menu with collapsible sub menu's, there are 3 levels of menu and it works fine but i don't know how to collapse all child sub levels when you click to collapse the parent one.
The way it works is that if you click level 1 it expands level 2, then if you click level 2 it expands level 3.
The issue is that when all levels are open, I need to be able to click level 1 and collapse both level 2 and 3 together. I am using slideToggle at the moment.
I have pasted my code below and I know it's a really rubbish way to do it but I have had to assign fake H tags to the menu items because it is generated by the ASP:Menu control and it gives no unique ID to the menu items so I had to find a way around it.
HTML:
<div id="Div1" class="MenuBar">
<a href="#Menu1_SkipLink" style="position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden;">Skip Navigation Links</a>
<div class="mainmenu" id="Div2">
<ul class="level1">
<li><a class="level1 staticItem level1">
<img src="/images/calculator.png" alt="" title="" class="icon" /><h7>Financial</h7></a></li>
<li><a class="level2 staticItem level2">
<h10>Address</h10>
</a></li>
<li><a class="level3 staticItem level3" href="javascript:openNewWin('/Controls/Financial/AddressBook.aspx')">
<h11>Address Book</h11>
</a></li>
<li><a class="level3 staticItem level3" href="javascript:openNewWin('/Controls/Financial/CustomerTypes.aspx')">
<h12>Customer Types</h12>
</a></li>
<li><a class="level1 staticItem level1">
<img src="/images/container.png" alt="" title="" class="icon" /><h8>Container</h8></a></li>
<li><a class="level2 staticItem level2">
<h13>Containers</h13>
</a></li>
<li><a class="level1 staticItem level1">
<img src="/images/product.png" alt="" title="" class="icon" /><h9>Product</h9></a></li>
</ul>
</div>
<a id="Menu1_SkipLink"></a>
</div>
JQuery:
$(function () {
//When opening the page all level 2 and level 3 items must be hidden.
$(function () {
hideitems();
})
function hideitems() {
$('h10').slideUp();
$('h11').slideUp();
$('h12').slideUp();
$('h13').slideUp();
}
//Financial Click
$(document).ready(function () {
$('h7').click(function () {
$('h10').slideToggle();
});
});
//Address Click
$(document).ready(function () {
$('h10').click(function () {
$('h11').slideToggle();
$('h12').slideToggle();
});
})
});
Any suggestions on how to collapse all sub levels would be greatly appreciated as I am no master of JS.
Without changing the markup : build a structure which links subitems to items, and use it in the .click()
callback.
For example, you can use $(...).data
to associate data with a node :
// init function :
// we stack all level2 and level3 items as subitems of the last level1 item we met
// we stack all level3 items as subitems of the last level2 item we met
function init() {
var $menu = $('ul.level1');
var lv1Itm = null,
lv2Itm = null;
$menu.find('a').each(function () {
var subitems;
var li = $(this).closest('li').get(0);
if ($(this).hasClass('level1')) {
lv1Itm = li;
$(li).data('subitems', []);
}
if ($(this).hasClass('level2')) {
lv2Itm = li;
$(li).data('subitems', []);
$(lv1Itm).data('subitems').push(li);
}
if ($(this).hasClass('level3')) {
$(lv1Itm).data('subitems').push(li);
$(lv2Itm).data('subitems').push(li);
}
});
}
init();
$('.level1').click(function (e) {
var subitems = $(this).closest('li').data('subitems');
var shouldHideSubmenus = $(subitems).is(':visible');
if (shouldHideSubmenus) {
// in this case, hide all 'li' children items
$(subitems).slideUp();
} else {
// in this case, only show level2 items
$(subitems).find('.level2').closest('li').slideDown();
}
});
$('.level2').click(function (e) {
var subitems = $(this).closest('li').data('subitems');
var shouldHideSubmenus = $(subitems).is(':visible');
if (shouldHideSubmenus) {
// in this case, hide all 'li' children items
$(subitems).slideUp();
} else {
// in this case, only show level3 items
$(subitems).find('.level3').closest('li').slideDown();
}
});
try to use this http://jsfiddle.net/modaloda/B8b4z/1/
//Financial Click
$(document).ready(function () {
$('h7').click(function () {
$('h10').slideToggle();
$('h11').slideUp("fast");
$('h12').slideUp("fast");
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.