this is my very first question.
I'm working on a fully Ajax system with jQuery, and it works fine with 1.6.2. When I tried to upgrade it to 1.7, this piece of code stopped working properly:
$("a[class!='']").live("click",function(e){
e.preventDefault();
});
In 1.6.2, it prevents all hyperlink tags from working as a link if they have a class, but in 1.7 it stopped ALL links from working as real links, even those without classes.
Fiddle: http://jsfiddle.net/hBehg/
Use $('a[class]')
, this will select all elements which have the class attribute. As I said in my comment, checking for an empty value might not work if the element does not even have a class
attribute.
Update: As pointed out by @Sidnicious, the documentation describes that this selector will also select those elements which do not have that attribute. If it didn't in 1.6, then it actually must have been a bug in that version, or they changed the description without mentioning it.
Of course, if you indeed have an empty class
attribute, i.e. <a class="">
, this will not work.
DEMO
Update 2: As @lonesomeday mentions in his comment, $('a[class][class!=""]')
does work as you intended with $(a[class!=""])
.
As others said, you can change to on
in jQuery 1.7, which unifies the event handling methods, but it won't solve your particular problem.
The jQuery docs describe the [name!=value] selector like this:
Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value.
In your case, it will select every a
which doesn’t have a class
or whose class is not equal to an empty string. <a></a>
and <a class=""></a>
are not the same.
That might actually have been a bug in jQuery 1.6!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With