问题
I have two menus: the Accordion menu and Tab menu, for the Tab menu, there is an assigned class selected to a link that is opened, I want to assign the class (for example) open_menu, in the same way for the accordion menu. actually if the class open_menu will be given from the condiotion that in its ul there is an A link with class Selected, it would be much better, because the class selected given to a link is taken from the cookies, and even if the page is refreshed it comes to this link Selected. Anyway, u can see all the source here: http://jsfiddle.net/bq6tA/6/ actually the end product i want, after the refresh of the page, there will be opened the accordion's tab, as well as the Tab's menu link selected that is in this accordion's tab
回答1:
See a working demo of the following code here.
I've modified your initMenu
function to add the open_menu
class to the appropriate accordion (and added a CSS class to indicate that it was added by changing the background to green):
function initMenu() {
// SNIP ...
$('#menu li a').click(function() {
// SNIP ...
if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#menu ul:visible').slideUp('normal')
.siblings('a').removeClass('open_menu');
checkElement.slideDown('normal')
.siblings('a').addClass('open_menu');
return false;
}
});
}
I then created a function to be called after initMenu
that will trigger a click on the accordion with the same rel
as the id
of the currently selected item:
function showCurrentTab() {
var curId = $('.tabcontent:visible')[0].id,
$curLink = $('a[rel="'+curId+'"]');
$curLink.closest('ul')
.parent('li')
.children('a').click();
}
To figure out what's going on here, see the API docs for closest, parent and children and relate that to your HTML structure.
来源:https://stackoverflow.com/questions/4836433/accordion-tab-menus-assign-select-class-for-both