Compared to the Attr,prop is 1.6.1 new, both from the Chinese meaning of understanding, are to get/set properties of the method (attributes and properties). Only the. attr () method used in window or document does not work well before jQuery1.6 because attributes is not available in window and document. Prop was born.
Before looking at the online comparison between the two articles, but also lists a table to distinguish what labels under the use of prop, what labels under the use of attr, forgive me is lazy people, the most afraid to back things, so only their own to think of ways.
Since we want to know the difference between them, it is best to look at their source code, not to be frightened by the length of the code, we only look at the key words:
attr Method Code (jquery version 1.8.3)
Attr:function (elem, name, value, pass) {var ret, hooks, notxml, ntype = Elem.nodetype;//don ' t get/set attributes on Text, comment and attribute nodes if (!elem | | ntype = 3 | | ntype = 8 | | ntype = 2) {return;} if (Pass &&A mp Jquery.isfunction (jquery.fn[name]) {return jQuery (elem) [name] (value);}//fallback to prop when attributes a Re not supported if (typeof Elem.getattribute = = "undefined") {return Jquery.prop (elem, name, value);} notxml = Nty PE!== 1 | | !jquery.isxmldoc (Elem); All attributes are lowercase//Grab necessary hooks if one is defined if (notxml) {name = Name.tolowercase (); hooks = jquery.attrhooks[Name] | | (Rboolean.test (name) boolhook:nodehook); } if (value!== undefined) {if (value = = null) {jquery.removeattr (elem, name); return;} else if (hooks &&am P "Set" in hooks && notxml && (ret = Hooks.set (elem, value, name))!== undefined) {return ret;} else { Elem.setattribute (NamE, Value + ""); return value; ' Else if ' (Hooks && ' get ' in hooks && notxml && (ret = Hooks.get (elem, name))!== null) {RE Turn ret; else {ret = Elem.getattribute (name);