1. 程式人生 > >JS如何通過url選中按鈕?

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語法