I found a lot of examples of dom operations on the Internet, and they felt incomplete and messy. You may often encounter this problem as follows:
1. Create dom elements
2. dom element insertion
3. dom element replacement
4. Delete dom elements
First, there are some simple dom elements on the page.
Aaaaaaaa
Bbbbbbbb
Ccccccccc
Next we will create a p element and js Code:
Var p_d = document. createElement ('P ');
P_d.innerHTML = "dddddddd ";
P_d.id = "d ";
// InnerText is not used here to avoid browser compatibility issues;
Then, insert the created p with the id d to the front of the p with the dom element id B.
Var p_wrap = document. getElementById ('wrapp ');
Var p_ B = document. getElementById ('B ');
P_wrap.insertBefore (p_d, p_ B );
If you directly insert a child element with the id of wrap, you can do this:
P_wrap.insertBefore (p_d, null );
If p_ B is replaced, it is as follows:
P_wrap.replaceChild (p_d, p_ B );
Finally, we delete the p with the specified Element id B.
P_ B .parentNode.removeChild (p_ B );
Or
Document. body. removeChild (p_ B );
There may be many derivative methods and applications in the future.