You should load the function on DOM ready. This can be done via jQuery's .ready()
, or its shorthand syntax, which I applied to the provided code:
jQuery(function($) {
var tab = $(".xmenu > .xmenu-nav > li > a"),
content = $(".xmenu > .xmenu-content > div");
tab.click(function (e) {
content.removeClass("active");
tab.removeClass("active");
$(this).addClass("active");
$($(this).attr("href")).addClass("active");
e.preventDefault();
});
});
This should load the tab switching script after your page has "initialised": after the DOM is ready to be used, which is exactly what you're using when you do something like $(".xmenu > .xmenu-nav > li > a")
.