Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 17:37:37
Original
1947 people have browsed it
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 );
};
};
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template