Set active link based on URL

后端 未结 6 1842
猫巷女王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条回答
  •  梦毁少年i
    2021-01-29 06:11

    You could get the current url in jQuery like this:

    $(location).attr('href');
    

    Then cut off everything after '?' with a regex, to get yor current page

    To add a class to the correct li-item:

    $('li.'+style+').addClass('active'); 
    

提交回复
热议问题