JavaScript manipulating DOM elements

Source: Internet
Author: User


1. Steps to create DOM dynamically

(1) Call the document's CreateElement method to create a DOM object with the specified label

(2) then add the newly created element to the appropriate element by calling the AppendChild () of the element.


2. How to manipulate the DOM

createelement (' element '); Create a node

Document.createelement (' <input type= ' button ' value= ' Hello '/> '), create elements quickly, and assign values, However, note that the inner section of the settings will not be set document.createelement (' <a href= ' www.baidu.com ' > Baidu </a> '); Note: "Baidu" is not written in the two words

AppendChild (node);

RemoveChild (node); Remove a node

ReplaceChild (new,old); Replace a node

InsertBefore (new, reference); Add a node to the front (plug in front of a node)

FirstChild

LastChild



3. Sample Code

Example code: Adding a DIV element

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/A0/wKioL1fF2meA-jYLAAEGwz-mCgU854.gif "title=" create _div.gif "alt=" Wkiol1ff2mea-jylaaegwz-mcgu854.gif "/>


Example code: Adding a DIV element (with style)

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/A0/wKioL1fF2tCC7K8ZAACXJYhgS2o574.gif "title=" create _div_with_style.gif "alt=" Wkiol1ff2tcc7k8zaacxjyhgs2o574.gif "/>


Example code: Adding a button and a text box to a DIV element

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">


One (Firefox second button can not be used, do not know why)

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/86/A0/wKiom1fF3PbSjZ-vAAE-_DDC4xI046.gif "title=" create _input_firefox.gif "alt=" Wkiom1ff3pbsjz-vaae-_ddc4xi046.gif "/>

Two

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/A0/wKioL1fF3QKwzkVFAAFoLcwWbUs127.gif "title=" create _input_ie.gif "alt=" Wkiol1ff3qkwzkvfaafolcwwbus127.gif "/>



Sample code: Delete all elements in div

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/A0/wKiom1fF3eDgAOzgAAFfmWd3xdQ621.gif "title=" create _input_ie_delete.gif "alt=" Wkiom1ff3edgaozgaaffmwd3xdq621.gif "/>






JavaScript manipulating DOM elements

Related Article

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.