jquery没有方法错误

时间:2012-04-04 09:43:57

标签: jquery jquery-selectors href addclass

我正在写一个ajax导航脚本。 我发现我可以使用

突出显示菜单中的当前部分
('a[href=' + document.location.hash + ']').addClass('current');

但我收到此错误:

Object a[href="#home"] has no method 'addClass'

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

缺少一些东西:

$('a[href="' + document.location.hash + '"]').addClass('current');

答案 1 :(得分:1)

您忘记了$标志?

尝试:

$('a[href=' + document.location.hash + ']').addClass('current');

如果没有$,JavaScript解析器会认为它是一个字符串对象。