what difference between 2 ,
b1.setattribute('id','b1'); and
b1.id='b1'; is 1 of them more efficient other ? , both of them same task ? , different in situations ?
difference between setattribute , htmlelement.attribute='value'
that latter bit, htmlelement.attribute='value', isn't quite accurate. you're not setting attribute there, you're setting property.
dom element instances in memory have various properties, some of connect or relate attributes , others don't.
attributes, on other hand, name/value pairs read directy html markup (and if serialize dom element, instance accessing innerhtml property, written markup back).
when property , attribute related/linked in way, property called reflected property (of attribute). sometimes, reflected property's name isn't quite same attribute's name (class becomes classname, for becomes htmlfor), , link between them isn't 1:1.
so instance, id reflected property of id attribute. select boxes have selectedindex property there's no attribute.
do both of them same task ?
and different in situations ?
it depends on attribute/property:
id, several others directly reflected: settingidproperty , settingidattribute same thing. offhand, true ofhtmlforproperty /forattribute (except on old ie has bugs insetattributethere),relproperty/attribute,classname/classattribute (except on old ie has bugs insetattributethere),nameattribute on form fields , other elements,method,actionproperties/attributes on forms, , several others.the
valueproperty, on other hand, doesn't setvalueattribute at all. gets default value it. on browsers ("all" @ point?), there's separatedefaultvalueproperty does directly reflectvalueattribute.the
hrefproperty differenthrefattribute in relation relative vs. absolute links. attribute can contain relative path, , usingstr = elm.getattribute("href")gives relative path; if read property (str = elm.href), absolute path (e.g., resolved path). settinghrefproperty relative path sets attribute path, again reading tehhrefproperty give absolute (resolved) version. settinghrefproperty absolute path set attribute absolute path.there several boolean properties represented booleans (true/false), since attribute values strings, attribute either not there false (
getattributereturnsnull) or there true. if it's there, must have value""or same name (e.g.,multiple="multiple", case-insensitive), although in practice browsers treat present attributetrueregardless of actual content.several properties aren't reflected in attributes @ all, setting them doesn't set/change attribute.
is 1 of them more efficient other ?
it's never going make big enough difference care, doesn't matter. varies dramatically browser.
Comments
Post a Comment