JS and JQ Insert/append/after/before add nodes and text __javascript

Source: Internet
Author: User

JQ Method:

Adding nodes and text using the JQ method is easier:

$ (' #div_one '). Append (' <span> new added 1</span> ');          Add after the last child node in the element (add without child nodes)  
$ (' <span> new added 2-1</span> '). Appendto ($ (' #div_one '));   Meaning ditto
$ (' <span> new added 2-2</span> '). Appendto (' #div_one ');     This notation can also be

$ (' <span> add 3</span> '). InsertBefore (' #span_one ');   Add sibling  
$ (' <span> new 4</span> ') before node. InsertAfter (' #span_one ');    Add sibling node after node  
$ (". Span-one"). Before ("<p>hello world,before span-one!</p>");//To each class= Span-one node before adding $ (". Span-one"). After ("<p>hello world,after span-one!</p>"); To add nodes after each class=span-one node

JS section:

var htmlnode = document.createelement (' book ');                  Named node is book (only node name can be defined and content cannot be added to the node)
document.getElementById (' Div_two '). appendchild (Htmlnode);       Add nodes after the last child node within the element <book></book> (add without child nodes)

var textnode = document.createTextNode (' <span> New add 5</span> ');                              Create nodes in text format (note that the <span> here is treated as text, not HTML tags)
document.getElementById (' Div_two '). getElementsByTagName (' Book ') [0].appendchild (Textnode);    Adds a node after the last child node within the element (added directly without child nodes)

var node = document.getElementById ("Div_one"). LastChild;        AppendChild can also be used to move node
document.getElementById ("Div_two"). appendchild (node);

document.getElementById (' Div_two '). InnerHTML = document.getElementById (' Div_two '). InnerHTML + ' <span> new Add 6 </span> ';    Add tags and content in HTML form

Complete code:

<!
            DOCTYPE html>  

The results are as follows:

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.