Set active link based on URL

后端 未结 6 1846
猫巷女王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:11

    function getStyleValue(uri)
    {
        var var, hash;
        var hashes = uri.slice(uri.indexOf('?') + 1).split('&');
    
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            if(hash[0] == 'style')
            {
                return hash[2];
            }
        }
        return false;
    }
    

    then you can use like

    var selected = getStyleValue('index.php?style=product2') || "product";
    $('a.' + selected).addClass('selected');
    

提交回复
热议问题