JS jQuery 의 append 기능 실현
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));
})
// append
this.appendChild(fragment);
// prepend
// this.insertBefore(fragment, this.firstChild);
nodes = null;
fragment = null;
};
효과 테스트html
<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);
효과.PS
또한,현재 위 에 삽입 하려 면 코드 에 있 는 this.append Child(fragment)만 필요 합 니 다.this.insertBefore(fragment,this.firstChild)로 변경 하기;
다른 방법
var div2 = document.querySelector("#div2");
div2.insertAdjacentHTML("beforebegin","<p>hello world</p>");//
div2.insertAdjacentHTML("afterbegin","<p>hello world</p>");//
div2.insertAdjacentHTML("beforeend","<p>hello world</p>");//
div2.insertAdjacentHTML("afterend","<p>hello world</p>");//
브 라 우 저의 렌 더 링 효과:이 방법 은 i 의 최초 방법 이기 때문에 호환성 이 매우 좋다.
이상 은 JS 가 jQuery 의 append 기능 을 실현 하 는 상세 한 내용 입 니 다.JS 가 jQuery append 를 실현 하 는 것 에 관 한 자 료 는 저희 의 다른 관련 글 을 주목 해 주 십시오!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.