Some suggestions:
Use
var
when declaring variables, unless you really want to make global variables (which you probably don't)Combine
var
statements using,
var a = 1, b = 2, c = 3;
Combine classes in
toggleClass
$(this).toggleClass('minus expand')
Cache selectors that are used often (like
$('#main')
)You don't need to use
$li2
inside the click handler, you can just use$(this)
Turn repeated code into a function
$(function(){
var $main = ('#main'),
slide = function(ele, find){
return $(ele).toggleClass('minus expand').find(find).delay(200).slideToggle(400);
},
$li = $('> li.expand', $main).click(function(){
var $ul = slide(this, 'ul.sub-level');
$('> li > ul', $main).not($ul).slideUp('slow');
$('> li.minus', $main).not(this).toggleClass('minus expand')
}),
$li2 = $('.sub-level > li.expand').click(function(){
slide(this, 'ul.sub-level2');
});
});