$(function() { var v = location.href, flag = false; $('ul.nav').find('.nav-item').each(function(item) { var h = $(this).find('.index').attr('href'); if (h == '/') return true; if (v.indexof(h) != -1) { $(this).addclass('active'); flag = true; } else { $(this).removeclass('active'); } }); if (!flag) { $('ul.nav').find('.nav-item').first().addclass('active'); } });