JQuery: prop vs attr ... clarification

Please note that this is not a duplicate of .prop () vs .attr () ; this question relates to the basic functionality of prop vs attr, while this question is specific to their differences as setters.

When trying to narrow down what should be installed using .prop (), vs, which should be installed via .attr () when creating a new element, in tests with jQuery 1.7.2, I find that prop is about 2.5 times faster, so it seems preferable.

The only list of things to install with attr that I could find gives them for attr:

accesskey, align, background, bgcolor, class, contenteditable, contextmenu, data-XXXX, draggable, height, hidden, id, item, itemprop, spellcheck, style, subject, tabindex, title, valign, width

  • is complete (i.e. everything that is not on this list - for example, min, max, step, etc. - should you use prop)?
  • during testing, some of them seem to work fine when installed with prop. Testing the above list, I had no problems setting it up:

    id, class, align, contenteditable, draggable, hidden, spellcheck, tabindex, title

  • Is there any reason for this list of "stuff to install with attr that still works when installing with prop", why should it be installed with attr anyway? If not, then when creating the base one <div id="foo" class="bar" draggable="true" title="zipzap">it seems to me more profitable 250% faster ... :)

+5
source share
2 answers

. , - , , jQuery 1.6. № 3, Starx sortof . http://timmywillison.com/ . MDN W3C , , , (https://developer.mozilla.org/en/DOM/element), MDN , . MDN , , getAttribute:

" HTML XML-, , HTML DOM, . , HTML- " [HTML 4.01] [ XHTML 1.0] XHTML ". HTML 5 , HTML-, , " , , , ". , DOM XML, getAttribute().

, HTML5, Firefox Chrome, , ", " ..

, , , jQuery, HTML, boolean, string int.

1.7.2 1.8pre, .prop() attr(), jQuery .prop :

async, autofocus, autoplay, checked, controls, defer, disabled, hidden, loop,
multiple, open, readonly, required, scoped, selected

HTML ( , .. ) jQuery , .attr():

accept-charset, accesskey, bgcolor, buffered, codebase, contextmenu, datetime,
default, dirname, dropzone, form, http-equiv, icon, ismap, itemprop, kind, 
language, list, location, manifest, nodeName, nodeType, novalidate, pubdate, 
radiogroup, seamless, selectedIndex, sizes, srclang, style, tagName

, , jQuery .prop() .attr(). jQuery .prop(), , .attr() .prop(). jQuery , . .prop(), jQuery .prop() vica versa. . , , , prop() ~ 2,5 , .attr(), jQuery 1.6.1 .attr(), .prop(), :

accept, action, align, alt, autocomplete, border, challenge, charset, cite, 
class, code, color, cols, colspan, contenteditable, coords, data, defaultValue, 
dir, draggable, enctype, for, headers, height, hidden, high, href, hreflang, 
id, keytype, label, lang, low, max, maxlength, media, method, min, name, 
optimum, pattern, ping, placeholder, poster, preload, readonly, rel, required, 
reversed, rows, rowspan, sandbox, scope, shape, size, span, spellcheck, src, 
srcdoc, start, step, summary, tabindex, target, title, type, usemap, value, 
width, wrap
+20

.

.attr() . , .

.prop(), ,

  • , , , , .
  • , .

question , . , DOM , . T.J. .

+1

All Articles