Javascriptdom basic operations summary _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.