JS如何通过url选中按钮?

我们很多时候需要通过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语法

猜你喜欢

转载自blog.csdn.net/qq_39429962/article/details/83756540
今日推荐