Home >Web Front-end >JS Tutorial >Introduction to using removeChild() to remove a node using javascript_javascript skills

Introduction to using removeChild() to remove a node using javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:53:251265browse

removeChild(a) is used to delete existing elements in the document
Parameter a: the node to be removed

Copy code The code is as follows:


1
2
< ;span>3


var oDiv = document.getElementById("guoDiv");
var firsChild = oDiv.firstElementChild ? oDiv.firstElementChild : oDiv. firstChild
var returnNode = oDiv.removeChild(firsChild); //Remove the first element and return the removed element
alert(returnNode.innerHTML); //1
var lastChild = oDiv.lastElementChild ? oDiv .lastElementChild : oDiv.lastChild;
returnNode = oDiv.removeChild(lastChild); //Remove the last element and return the removed element
alert(returnNode.innerHTML);//3
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn