Walking the DOM
輕而易舉用原生 code manipulate element nodes
Select element
Insert element
Change Some content
Styles and classes
Attribute
HTML attributes have the following features:
Their name is case-insensitive (
id
is same asID
).Their values are always strings, but properties are not always strings. For instance, the
input.checked
property (for checkboxes) is a boolean:
elem.hasAttribute(name)
– checks for existence.elem.getAttribute(name)
– gets the value.elem.setAttribute(name, value)
– sets the value.elem.removeAttribute(name)
– removes the attributeele.attributes
- a collection of objects that belong to a built-in Attr class, withname
andvalue
properties.
dataset
All attributes starting with “data-” are reserved for programmers’ use. They are available in the dataset
property.
Last updated