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: