Home  >  Article  >  Web Front-end  >  Native js implements appending content to the end of the specified element_javascript skills

Native js implements appending content to the end of the specified element_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:37:371922browse
Copy code The code is as follows:

var header1 = document.getElementById("header");
var p = document.createElement("p"); // Create an element node
insertAfter(p, header1); // Because js does not have a method to directly append to the specified element, you have to create a method yourself
function insertAfter( newElement, targetElement ){ // newElement is the element to be appended targetElement is the position of the specified element
var parent = targetElement.parentNode; // Find the parent node of the specified element
if( parent.lastChild == targetElement ){ // Determine whether the specified element is the last position in the node. If so, use the appendChild method directly
parent.appendChild( newElement, targetElement );
}else{
parent.insertBefore( newElement, targetElement.nextSibling );
};
};
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