Show Me The CodeHTMLElement.prototype.appendHTML = function(html) { let divTemp = document.createElement("div"); let nodes = null; let fragment = document.createDocumentFragment(); divTemp.innerHTML = html; nodes = divTemp.childNodes; nodes.forEach(item => { fragment.appendChild(item.cloneNode(true)); }) // Insert to the end append this.appendChild(fragment); // Insert prepend at the front // this.insertBefore(fragment, this.firstChild); nodes = null; fragment = null; }; Test the effecthtml <style> .child { height: 50px; width: 50px; background: #66CCFF; margin-bottom: 1em; } </style> <div id="app"> <div class="child"> <div class="child"> </div> js let app = document.getElementById('app'); let child = `<div class="child">down</div>`; app.appendHTML(child); Effect PSIn addition, if you want to insert above, just change this.appendChild(fragment); in the code to this.insertBefore(fragment, this.firstChild); Another approachvar div2 = document.querySelector("#div2"); div2.insertAdjacentHTML("beforebegin","<p>hello world</p>");//Add an element before the calling elementdiv2.insertAdjacentHTML("afterbegin","<p>hello world</p>");//Add a child element inside the calling element and replace the first child elementdiv2.insertAdjacentHTML("beforeend","<p>hello world</p>");//Add a child element after the calling element and replace the last child elementdiv2.insertAdjacentHTML("afterend","<p>hello world</p>");//Add an element after the calling element The effect of browser rendering: This method is the earliest method of IE, so the compatibility is particularly good The above is the details of JS implementing jQuery's append function. For more information about JS implementing jQuery append, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to set up the terminal to run applications after Ubuntu starts
>>: How InnoDB cleverly implements transaction isolation levels
Table of contents 1. Introduction 2. Actual Cases...
Preface Engineers working in the Linux environmen...
3. MySQL Data Management The first method: not re...
Preface: Use debugbar to view document code in iet...
This command modifies the data table ff_vod and a...
Browser compatibility is nothing more than style ...
1. Search for redis image docker search redis 2. ...
1. Background execution Generally, programs on Li...
Preface As one of the best web servers in the wor...
This article example shares the specific code of ...
1. iframe definition and usage The iframe element...
I believe that the Internet has become an increas...
Preface This article mainly introduces the releva...
Skip the Docker installation steps 1. Pull the po...
This article records the detailed installation pr...