Attribute selector a[href=#example] no longer working after updating jQuery [duplicate]

拈花ヽ惹草 提交于 2019-12-24 09:47:50

问题


$('a[href=#InterventionEditDocs]').trigger('click');

This line of code was working for my project perfectly a few days back but after some updates or something it throws an error in the console.

I fixed it by adding quotes around the value:

$('a[href="#InterventionEditDocs"]').trigger('click');

and it is now working. But why was it working before, and why do I have to quote the value for it to work again?


回答1:


But why was it working before

This was a bug with jQuery that was fixed in 1.12.0 and 2.2.0.

a[href=#InterventionEditDocs] is an invalid CSS selector, because # is a special character (representing an ID selector), and therefore cannot appear in an ident. It never should have worked in jQuery in the first place, and would have resulted in a SYNTAX_ERR if put through document.querySelectorAll().

and why do I have to quote the value for it to work again?

a[href="#InterventionEditDocs"] is valid, because the attribute value is now quoted, and therefore no longer an ident but a string.



来源:https://stackoverflow.com/questions/41973243/attribute-selector-ahref-example-no-longer-working-after-updating-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!