table of Contents
Talk about redrawing and rearranging
- Rearrangement and redrawing are one of the main reasons for energy consumption in DOM programming.
- Rearrangements (reflux): When
render tree
a part or all of the elements because of the size, the layout, hidden, and so change caused by re-rendering of the page, a process referred to as rearrangement. - Redraw: When the
render tree
updated attributes in the (render tree) only affect the appearance and style of the element, but not the layout of the element, the browser needs to redraw the style of the current element, which is called redrawing. - Redrawing will not cause rearrangement, but rearrangement will definitely cause redrawing. The rearrangement of an element usually brings about a series of reactions, and even triggers the rearrangement and redrawing of the entire document. The performance cost is high.
Respectively describe innerHTML innerText textContent
-
innerHTML: When obtaining, the label is obtained as a string.
- When set, the tags in the string are directly parsed.
-
innerText: When getting, only get the content of the text node in the element.
- When set, the tags in the string will not be parsed.
-
textContent: When getting, only get the content of the text node in the element.
- When set, the tags in the string will not be parsed.
The old method is set to get and delete the attribute method
-
Own attributes
- Directly through
元素对象.属性名
to get. - If some of its own attributes conflict with the keywords of js, then the method of obtaining the current own attributes will be modified. For
example, when the class attribute is obtained, it can beclassName
obtained.
- Directly through
-
Custom attributes
- Obtain:
- Can not directly
元素对象.属性名
get to, because he did not find the property on its own property of the current element, the current element will be the object as a js object to gain his property up. - The user-defined attribute must be
getAttribute
searched before the current self-attribute can be found on the attribute of the element.
- Can not directly
- Settings:
setAttribute(key,value)
- delete:
removeAttribute(key)
- Obtain:
h5 custom attributes
-
h5 stipulates:
- If it is a custom attribute, please add a
data-*
prefix. - h5 provides an
dataset
attribute for each element , which is an object, which saves all the custom attributes of the current element. - As long as
dataset
you manipulate properties on this object, you are manipulating custom properties for the current element.
- If it is a custom attribute, please add a
-
Set h5 custom attributes to
useele.dataset.key = value
-
Get custom attributes
useele.dataset.key
-
Delete custom attributes
usedelete ele.dataset.key
Create element node insert element node copy node delete node replace node operation
-
Create an element node:
document.createElement("元素名")
;
Special:new Image()
Create an img tag. -
Insert node:
A.appendChild(B);
Insert a B node at the end of the inside of the A element.A.insertBefore(new,old);
Insert a new directly in front of the old element. New and old are brothers.
A is the parent element node of old and new.
-
A.cloneNode();
- Copy node A.
- The parameter is a boolean and the default is false.
- false means shallow copy only copies the current element.
- true is to deep copy the current element and all elements inside.
-
Delete element:
A.removeChild(B)
delete the child element B in the A element. -
Replace element:
A.replaceChild(new,old)
replace old with new
A is the parent element of new and old.