When writing HTML source code, you can define attributes on your HTML elements. Then, once the browser parses your code, a corresponding DOM node will be created. This node is an object, and therefore it has properties.
For instance, this HTML element:
<input type="text" value="Name:">
has 2 attributes (type and value).
Once the browser parses this code, a HTMLInputElement object will be created, and this object will contain dozens of properties like: accept, accessKey, align, alt, attributes, autofocus, baseURI, checked, childElementCount, childNodes, children, classList, className, clientHeight, etc.
For a given DOM node object, properties are the properties of that object, and attributes are the elements of the attributes property of that object. 对于一个DOM节点对象,property就是这个对象上的属性;attribute是该对象对应的HTML标签元素上的属性。
When a DOM node is created for a given HTML element, many of its properties relate to attributes with the same or similar names, but it's not a one-to-one relationship. For instance, for this HTML element:
<input id="the-input" type="text" value="Name:">
the corresponding DOM node will have id,type, and value properties (among others):
-
The
idproperty is a reflected property for theidattribute: Getting the property reads the attribute value, and setting the property writes the attribute value.idis a pure reflected property, it doesn't modify or limit the value. -
The
typeproperty is a reflected property for thetypeattribute: Getting the property reads the attribute value, and setting the property writes the attribute value.typeisn't a pure reflected property because it's limited to known values (e.g., the valid types of an input). If you had<input type="foo">, thentheInput.getAttribute("type")gives you"foo"buttheInput.typegives you"text". -
In contrast, the
valueproperty doesn't reflect thevalueattribute. Instead, it's the current value of the input. When the user manually changes the value of the input box, thevalueproperty will reflect this change. So if the user inputs"John"into the input box, then:theInput.value // returns "John"whereas:
theInput.getAttribute('value') // returns "Name:"The
valueproperty reflects the current text-content inside the input box, whereas thevalueattribute contains the initial text-content of thevalueattribute from the HTML source code.So if you want to know what's currently inside the text-box, read the property. If you, however, want to know what the initial value of the text-box was, read the attribute. Or you can use the
defaultValueproperty, which is a pure reflection of thevalueattribute:theInput.value // returns "John" theInput.getAttribute('value') // returns "Name:" theInput.defaultValue // returns "Name:"
There are several properties that directly reflect their attribute (rel, id), some are direct reflections with slightly-different names (htmlFor reflects the for attribute, className reflects the class attribute), many that reflect their attribute but with restrictions/modifications (src, href, disabled, multiple), and so on.
文章讲述了HTML元素的属性如何在DOM节点中体现为对象的属性。例如,<input>元素的id、type和value在DOM节点中都有对应属性。id和type是纯反射属性,而value属性反映的是输入框的当前值,不完全等同于HTML源码中的初始值。defaultValue属性则代表了元素的初始值。文章还提到了其他属性与它们的限制或修改情况。
305

被折叠的 条评论
为什么被折叠?



