Set active link based on URL

后端 未结 6 1900
猫巷女王i
猫巷女王i 2021-01-29 05:58

If the URL ends: ?style=product I want to add a class of active to the li with the class of product

HTML:

<         


        
6条回答
  •  甜味超标
    2021-01-29 06:10

    use location.href to read the query parameters...

       function getQuerystring(key, default_)  {
          if (default_==null) default_="";
          key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
          var qs = regex.exec(window.location.href);
          if(qs == null)
            return default_;
          else
            return qs[1];
        }
    
        var target = $("." + getQueryString("style", "default"));
    

提交回复
热议问题