jQuery 未捕获异常:语法错误,无法识别的表达式 [tabindex="something"]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5904146/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:59:40  来源:igfitidea点击:

jQuery uncaught exception: syntax error, unrecognized expression [ tabindex="something"]

jqueryhtmljquery-selectors

提问by IntricatePixels

All of a sudden some UI functionlities in our site are not working and I'm getting the error message:

突然之间,我们网站中的一些 UI 功能无法正常工作,我收到了错误消息:

jQuery uncaught exception: syntax error, unrecognized expression [ tabindex="something"]

jQuery 未捕获异常:语法错误,无法识别的表达式 [tabindex="something"]

THIS IS MY CODE:

这是我的代码:

var thumb_src = jQuery('a[name="thumb-image"] img[src*=' + sku + ']').attr('src');
jQuery( 'a[ tabindex=' + thumb_src + ']' ).prevAll().removeClass('selectedThumb');
jQuery( 'a[ tabindex=' + thumb_src + ']' ).addClass( 'selectedThumb' );
jQuery( 'a[ tabindex=' + thumb_src + ']' ).nextAll().removeClass('selectedThumb');

It was working fine until jQuery was upgraded to the latest and I believe that is the cause. Am I doing something illegal in the statements above? Thanks for any input or help on this!

它运行良好,直到 jQuery 升级到最新版本,我相信这就是原因。我在上面的陈述中做了什么违法的事情吗?感谢您对此提供任何意见或帮助!

回答by BoltClock

Most likely any .or /characters in your thumb_srcare breaking the attribute selectors in your last three lines as they are special CSS characters.

很可能您中的任何./字符thumb_src都破坏了最后三行中的属性选择器,因为它们是特殊的 CSS 字符。

Try the double quotes inside those selectors so they're taken literally (even though you really shouldn't be using anything but numeric values for tabindex):

尝试在这些选择器中使用双引号,以便按字面意思理解它们(即使您真的不应该使用除数字值之外的任何东西tabindex):

jQuery('a[tabindex="' + thumb_src + '"]')

The API docssay that these quotes are mandatory in jQuery attribute selectors anyway.

API文档说,这些报价都在jQuery的属性选择器的强制性反正。

回答by Naftali aka Neal

The attr()function was changed as of jQuery 1.6, use prop()instead:

attr()函数从jQuery 1.6开始更改,请prop()改用:

var thumb_src = jQuery('a[name="thumb-image"] img[src*=' + sku + ']').prop('src');

See this question

看到这个问题