JavaScript elements, nodes

var btn1 = document.getElementById("btn1"); btn1.onclick = function()                 { /*// create P element var p = document.createElement("p"); // create text var txt = document.createTextNode( "I am the created text"); // Append the text to the P element p.appendChild(txt);*/ var li = document.createElement("li"); var item1 = document.createTextNode("Item1 "); li.appendChild(item1); /*// Get div var div = document.getElementById("mydiv"); // Insert p into div div.appendChild(p);*/ div.appendChild(li ); } // get list id var ul = document.getElementById("cc"); // get button id and add onclick event
































var btn2 = document.getElementById("btn2");
btn2.onclick = function()
               {
// if ul has children
if(ul.hasChildNodes())
                        {
// get the last child in the list
var li = ul. lastChild;
// the name of the node is equal to itself
// get the last li
while(li.nodeName=='#text')
                                {
//previousSibling property can return the node immediately before a node 
li = li.previousSibling;
}
// delete node
ul.removeChild(li);
} }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324874423&siteId=291194637