jquery selectors
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.
精彩评论