我们很多时候需要通过url选中某一个按钮,这时候就需要获取url之后,然后通过jq选中,代码如下:
$(function(){
var h = window.location.href;
$("#ul li").removeClass("active");
$("#ul li").each(function(){
var currenthref = $(this).children("a").attr("href");
var n = currenthref.indexOf(".html");
currenthref = currenthref.substring(0,n+5);
console.log(currenthref);
if(h.indexOf(currenthref)!=-1)
$(this).addClass("active");
if(h.indexOf("userLogin")!=-1)
$("#first").addClass("active");
});
});
ps:注意一定要引用jquery文件,否则不能使用jq语法