Can you add tags to javascript?
This article mainly introduces whether javascript can add tags, the article is very detailed, has a certain reference value, interested friends must read it!
Javascript can add tags, method: 1, use the "document.createElement" statement to create a new tag node; 2, use the insertBefore () or appendChild () function to insert a new tag node before or after the specified child element node.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
The insertion of a node can be divided into two cases: appending a child node to the element's child node list and inserting a child node in front of one of the element's child nodes:
The first case calls: element.appendChild (child node)
The second case calls: element.insertBefore (new node, existing node).
Example 1: append child nodes to the list of element child nodes
Coffee Tea clicks the button to add the item to the list
Click function myFunction () {var newItem = document.createElement ("LI") var textnode = document.createTextNode ("Water") newItem.appendChild (textnode) Var list = document.getElementById ("myList") list.insertBefore (newItem List.childNodes [0]) }
Note:
First create a li node
Then create a text node
Then add the text node in the li node.
Finally, insert the li node in the first child node list.
The above is all the content of the article "can javascript add tags". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!