问题
what would be the difference between the two ,
b1.setAttribute('id','b1');
and
b1.id='b1';
is one of them more efficient than the other ? and do both of them will do exactly the same task ? and will they be different in some situations ?
回答1:
difference between setAttribute and htmlElement.attribute='value'
That latter bit, htmlElement.attribute='value'
, isn't quite accurate. You're not setting an attribute there, you're setting a property.
DOM element instances in memory have various properties, some of which connect to or relate to attributes and others which don't.
Attributes, on the other hand, are name/value pairs that are read directy from the HTML markup (and if you serialize a DOM element, for instance by accessing its innerHTML
property, are written to the markup you get back).
When the property and attribute are related/linked in some way, the property is called a reflected property (of the attribute). Sometimes, the reflected property's name isn't quite the same as the attribute's name (class
becomes className
, for
becomes htmlFor
), and sometimes the link between them isn't 1:1.
So for instance, id
is a reflected property of the id
attribute. But select boxes have a selectedIndex
property for which there's no attribute.
do both of them will do exactly the same task ?
and will they be different in some situations ?
It depends on the attribute/property:
id
and several others are directly reflected: Setting theid
property and setting theid
attribute do exactly the same thing. Offhand, this is also true of thehtmlFor
property /for
attribute (except on old IE which has bugs insetAttribute
there), therel
property/attribute, theclassName
/class
attribute (except on old IE which has bugs insetAttribute
there), thename
attribute on form fields and some other elements, themethod
andaction
properties/attributes on forms, and several others.The
value
property, on the other hand, doesn't set thevalue
attribute at all. It just gets its default value from it. On most browsers ("all" at this point?), there's a separatedefaultValue
property which does directly reflect thevalue
attribute.The
href
property is slightly different from thehref
attribute in relation to relative vs. absolute links. The attribute can contain a relative path, and usingstr = elm.getAttribute("href")
gives you that relative path; if you read the property (str = elm.href
), it will always be an absolute path (e.g., the resolved path). Setting thehref
property to a relative path sets the attribute to that path, but again reading tehhref
property will give you the absolute (resolved) version. Setting thehref
property to an absolute path will set the attribute to that absolute path.There are several boolean properties which are represented as booleans (true/false), but since attribute values are always strings, the attribute is either not there for false (
getAttribute
returnsnull
) or there for true. If it's there, it must have the value""
or the same as its name (e.g.,multiple="multiple"
, case-insensitive), although in practice browsers treat any present attribute astrue
regardless of its actual content.Several properties aren't reflected in attributes at all, so setting them doesn't set/change any attribute.
is one of them more efficient than the other ?
It's never going to make a big enough difference to care, so it doesn't matter. It also probably varies dramatically by browser.
来源:https://stackoverflow.com/questions/32793811/difference-between-setattribute-and-htmlelement-attribute-value