Operations on DOM by js and jquery

Source: Internet
Author: User

1. Create an element node
The traditional javascript method creates element nodes.
Var a = document. createElement ("p ");
The method for creating a node in jQuery is:
$ ('<P> </p> ');
Like createElement (), the newly created element node is not automatically added to the document.
If you want to add it to the document, you can use methods such as append (), insertAfter (), and before () in jQuery.
For example:
Var a = $ ('<p> </p> ');
$ ('Body'). append (a); // Add it to the end of the body element.

2. Create a text node:
The traditional javascript method creates text nodes
Var B = document. createTextNode ("my demo ");
It is usually used together to create a text node and an element node.
For example:
Var mes = document. createTextNode ("hello world ");
Var container = document. createElement ("p ");
Container. appendChild (mes );
Document. body. appendChild (container );

Creating a node in jQuery does not have to be so troublesome:
$ ('<P> hello world </p> ');
Like createElement (), the newly created element node is not automatically added to the document.
If you want to add it to the document, you can use methods such as append (), insertAfter (), and before () in jQuery.
For example:
Var a = $ ('<p> hello world </p> ');
$ ('Body'). append (a); // Add it to the end of the body element.

3. Copy a node
The traditional javascript method copies nodes:
For example:
Var mes = document. createTextNode ("hello world ");
Var container = document. createElement ("p ");
Container. appendChild (mes );
Document. body. appendChild (container );
Var newpara = container. cloneNode (true); // difference between true and false
Document. body. appendChild (newpara );
Note:
True: <p> aaaa </p> clone.
False: Only <p> </p> is cloned, and the text in it is not cloned.
You can use firebug.

Copy nodes in jQuery:
Var a = $ ('<p> hello world </p> ');
$ ('Body'). append ();
Var clone_a = a. clone ();
$ ('Body'). append (clone_a );

Like createElement (), the copied new element node is not automatically added to the document.
If you want to add it to the document, you can use methods such as append (), insertAfter (), and before () in jQuery.
Note: If the id is the same after cloning, do not forget to use. attr ("id", "new_id") to change the ID of the new node.

4. Insert nodes
Insert nodes in the traditional javascript method:
For example:
AppendChild ():
Append a subnode to the element, and insert the new node to the end.
Var container = document. createElement ("p ");
Document. body. appendChild (container );

InsertBefore ():
As the name implies, a new node is inserted before the target node.
Element. insertBefore (newNode, targerNode );

Insert nodes in jQuery is much more than that in javascript,
For example:
. Append ()
. AppendTo ()
. Prepend ()
. PrependTo ()
. After ()
. InsertAfter ()
. Before ()
. InsertBefore ()
Therefore, simplifying dom operations is also one of jquery's highlights.

5. delete a node
Delete a node using the traditional javascript method:
For example:
Var B = document. getElementById ("B ");
Var c = B. parentNode;
C. removeChild (B );

Delete a node in jQuery:
For example:
$ ('# Test2'). remove ();

6. Replace nodes
The traditional javascript method replaces nodes:
For example:
Element. repalceChild (newNode, oldNode );
The oldNode must be a subnode of the Element.

Replace nodes in jQuery:
For example:
$ ("<P> replace test1! </P> "). replaceAll (" # test1 ");

7. Set Properties to get Properties
In the traditional javascript method, set attributes and obtain attributes:
For example:
SetAttribute (); // set
Var a = document. createElement ("p ");
A. setAttribute ("title", "my demo ");
Whether the title attribute exists or not, the subsequent value is my demo.

GetAttribute (); // get
Var a = document. getElementById ("cssrain ");
Var B = a. getAttribute ("title ");
If the property does not exist, null is returned,

In jQuery, set attributes to get attributes:
For example:
$ ("# Test1"). attr ({"class": "test", "title": "TestDemo! "});
$ ("# Test1"). attr ("class ");

8. Search for nodes
Finding nodes is a piece of cake for jQuery.
JQuery is most concerned with finding nodes, that is, the selector.
For example:
$ ('# Id ')
$ ('. Class ')
$ ('Tag ')
$ ('Tag. class ')
$ ('# Id tag ')
$ ('Tag # id ')
$ ('# Id: visible ')
$ ('# Id. class ')
$ ('. Class. class ')
....

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.