javascript 未捕获的异常:语法错误,无法识别的表达式:[href=example.html]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5818555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uncaught exception: Syntax error, unrecognized expression: [href=example.html]
提问by Jeroen
While testing my website in Firebug i get this error when clicking on a menu button:
在 Firebug 中测试我的网站时,单击菜单按钮时出现此错误:
uncaught exception: Syntax error, unrecognized expression: [href=schedule.html]
未捕获的异常:语法错误,无法识别的表达式:[href=schedule.html]
I think it goes wrong here because the current class won't apply but the rest works fine.(these aren't the full code)
我认为这里出错了,因为当前的类不适用,但其余的工作正常。(这些不是完整的代码)
html:
html:
<nav>
<ul>
<li><a class="current" href="index.html">HOME</a></li>
<li><a href="schedule.html">SCHEDULE</a></li>
</ul>
</nav>
js:
js:
$("nav a").removeClass("current");
$("nav a[href="+newHash+"]").addClass("current");
回答by Richard Neil Ilagan
This looks like your culprit:
这看起来像你的罪魁祸首:
// add single quotes on your selector value
$("nav a[href='"+newHash+"']").addClass("current");
回答by Edgar Villegas Alvarado
Since jquery 1.5, quoting attribute values is mandatory. You can quote with single or double quotes:
从 jquery 1.5 开始,引用属性值是强制性的。您可以用单引号或双引号引用:
$("nav a[href='"+newHash+"']").addClass("current");
or
或者
$('nav a[href="'+newHash+'"]').addClass("current");
Quoting was optional in jQuery 1.4 or lower.
在 jQuery 1.4 或更低版本中,引用是可选的。