> I've read through both the link you suggested > andhttp://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips... > but I can't understand how I would make event delegation work for me. > > This is what I attempted: > > function watchLinks() > { > $("a").click(function(e) > { > var linkClicked = $(e.target); > if( linkClicked.is("a")) > { > var youWantToGoTo = > linkClicked.attr('href').substr(0,$(this).attr('href').length-4); // > Determine destination > openPage(youWantToGoTo); // Open destination > window.location.hash = youWantToGoTo; // Set the url # > return false; > } > }); > };
Don't bind the anchors, bind the document: $(document).click(function(e) { var $el = $(e.target); if ($el.is('a')) { var href = $el.attr('href'); var hash = href.substr(0,href.length-4); openPage(hash); window.location.hash = hash; return false; } });