$el.append() can be used to add a string DOM to an element in jQuery, but in Vue it’s not appropriate to use jQuery anymore, so wrap up a method that does the same thing.

Show Me The Code

HTMLElement.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 last append
	this.appendChild(fragment);
	// Insert prepend at the front
	// this.insertBefore(fragment, this.firstChild);
	nodes = null;
	fragment = null;
};
Copy the code

Let’s test the effect

html

<style>
.child {
  height: 50px;
  width: 50px;
  background: #66CCFF;
  margin-bottom: 1em;
}
</style>

<div id="app">
  <div class="child">
  <div class="child">
</div>
Copy the code

script

let app = document.getElementById('app');
let child = `<div class="child">down</div>`;
app.appendHTML(child);
Copy the code

The effect

PS

Add this. AppendChild (fragment) in the code if you want to insert above; Instead this. InsertBefore (fragments, enclosing firstChild);