最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery attribute selector unrecognized expression error - Stack Overflow

programmeradmin2浏览0评论

I'm using the following code to select the list item for current page from a navigation unordered list, but I keep getting an unrecognized expression error on 'a[href$="{server-relative URL}"]'

I've checked matching quotes/brackets that other questions mention, and the link will not contain characters beyond the usual / & ? % The code I'm using is:

$(document).ready(function() {
    var pathname = window.location.pathname;
    var selector = "'a[href$=\"" + pathname + "\"]'";
    var listItem = $(selector).parent().parent();
    listItem.addClass('selected');
});

I'm using jQuery 1.8.2 (latest version). Thanks!

I'm using the following code to select the list item for current page from a navigation unordered list, but I keep getting an unrecognized expression error on 'a[href$="{server-relative URL}"]'

I've checked matching quotes/brackets that other questions mention, and the link will not contain characters beyond the usual / & ? % The code I'm using is:

$(document).ready(function() {
    var pathname = window.location.pathname;
    var selector = "'a[href$=\"" + pathname + "\"]'";
    var listItem = $(selector).parent().parent();
    listItem.addClass('selected');
});

I'm using jQuery 1.8.2 (latest version). Thanks!

Share Improve this question asked Nov 13, 2012 at 0:11 MorayMMorayM 2,6877 gold badges33 silver badges55 bronze badges 2
  • 1 Why "', when only one delimiter (either " or ') should be enough? – raina77ow Commented Nov 13, 2012 at 0:15
  • 1 Double check your quotes, that's the problem right there. – elclanrs Commented Nov 13, 2012 at 0:16
Add a ment  | 

1 Answer 1

Reset to default 6

The problem is that you're surrounding your selector in single quotes. Just change this line:

var selector = "'a[href$=\"" + pathname + "\"]'";

...to this:

var selector = 'a[href$="' + pathname + '"]';
发布评论

评论列表(0)

  1. 暂无评论