开发者

jquery selectors

开发者 https://www.devze.com 2022-12-15 00:07 出处:网络
I want to add class to <a>开发者_StackOverflow中文版, what has some url. For exampe : $(document).ready(function() {

I want to add class to <a>开发者_StackOverflow中文版, what has some url. For exampe :

$(document).ready(function() {

    var pathname = window.location.pathname;                    
    $(".navholder ul li a").each(function() {
        $(this).addClass('sel');
    });

});

I want to add "sel" class to <a> what has href=pathname.

Theoretically something like this :

$(".navholder ul li a").attr('href', pathname).addClass('sel');

Thanks!


Use this line:

$(".navholder ul li a[href='"+ pathname +"']").addClass('sel');


Take a look at the jQuery selector syntax:

$('.navholder ul li a [href=\\/foo\\/bar]').addClass('sel');

Note that you must escape / in jQuery expressions, and double-escape if it is a string literal.

0

精彩评论

暂无评论...
验证码 换一张
取 消