var urlstr = location.href; var urlstatus=false; console.log(urlstr); $("#navbar ul li a").each(function () { if ((urlstr + '/').indexof($(this).attr('href')) > -1&&$(this).attr('href')!='') { $(this).parent().addclass('active'); urlstatus = true; } else { $(this).parent().removeclass('active'); } }); if (!urlstatus) {$("#navbar ul li").eq(0).addclass('active'); }