I'm new to jquery and javascript, and I'm doing a site with a drop-down accordion menu. It actually has two drop-down menus on the page, a horizontal one along the top, and a vertical side menu.
The default menu code uses a .click() event on a "li" to initiate .slideToggle and .slideUp actions. This is fine for the side menu, but the top menu requires a mouse roll-over event to trigger the sliding.
Not really knowing what to do, I duplicated the code for the .click menu, and changed .click to .mouseover for the top menu. This worked fine in Firefox and Chrome, but in IE7 & 8 I got an "Exception thrown and not caught error" whenever I moused over one of the sub menu items in the .click sliding menu.
function initMenuSide() {
$('ul.#menu ul').hide();
$.each($('ul.#menu'), function(){
$('#' + this.id + '.expandfirst ul:first').show();
});
$('ul.#menu li a').click(
function() {
var checkElement = $(this).next();
var parent = this.parentNode.parentNode.id;
if($('#' + parent).hasClass('noaccordion')) {
$(this).next().slideToggle('normal');
return false;
}
if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
if($('#' + parent).hasClass('collapsible')) {
$('#' + parent + ' ul:visible').slideUp('normal');
}
return false;
}
if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#' + parent + ' ul:visible').slideUp('normal');
checkElement.slideDown('normal');
return false;
}
}
);
}
function initMenuTop() {
$('ul.#menuTop ul').hide();
$.each($('ul.#menuTop'), function(){
$('#' + this.id + '.expandfirst ul:first').show();
});
$('ul.#menuTop li a').mouseenter(
function() {
var checkElement = $(this).next();
var parent = this.parentNode.parentNode.id;
if($('#' + parent).hasClass('noaccordion')) {
$(this).next().slideToggle('normal');
return false;
}
if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
if($('#' + parent).hasClass('collapsible')) {
$('#' + parent + ' ul:visible').slideUp('normal');
}
return false;
}
if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#' + parent + ' ul:visible').slideUp('normal');
checkElement.slideDown('normal');
return false;
}
}
);
}
$(document).ready(function() {initMenuSide();});
$(document).ready(function() {initMenuTop();});