问题
$('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