$ (". Fengye a"). each (function () { if (typeof().attr ("href") ) = "undefined") { $ (this). AddClass ("Thisa"); } Else { $ (this). Removeclass ("Thisa"); } );
. Fengye { background:#106bb1; color:#fff; }
In jquery coding, we determine whether an element has a property or not, such as whether it contains a class= "new" style. jquery is very simple to judge, because there is hasclass this method $ ("input[name=new]"). Hasclass ("new") can be judged.
But sometimes we need to judge other properties, such as a link containing the Rel attribute, and some without the Rel attribute. How do you judge this?
Then there is no ready-made method. If there is a property of $ ("#aid"). attr ("rel") returns the value of rel and returns "undefined" if no rel attribute exists
Undefined is the undefined type, if ($ ("#aid"). attr ("rel") = = = "undefined") this judgment may not be tenable.
Because the types are not the same.
It is recommended to use if (typeof ("#aid"). attr ("rel")) = = "undefined")
Final effect
JQuery determines whether a property exists hasattr