国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

JS實現jQuery的append功能

瀏覽:87日期:2024-03-24 08:07:24
目錄Show Me The Code測試下效果效果PS另一種方法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));})// 插入到最后 appendthis.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);效果

JS實現jQuery的append功能

PS

另外, 如果想實現在上方插入的話, 只需要把代碼里的this.appendChild(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>');//在調用元素的外部后面添加一個元素

瀏覽器的渲染的效果:

JS實現jQuery的append功能

此方法是ie 的最早的方法所以兼容性特別好

以上就是JS實現jQuery的append功能的詳細內容,更多關于JS 實現jQuery append的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 洪泽县| 日照市| 河曲县| 峨边| 新宾| 固原市| 吴川市| 正镶白旗| 庐江县| 北宁市| 淮滨县| 毕节市| 贵溪市| 大同市| 个旧市| 富川| 津市市| 枣强县| 洛宁县| 习水县| 固阳县| 长沙县| 若尔盖县| 白朗县| 喀喇沁旗| 新蔡县| 醴陵市| 舒城县| 青阳县| 大埔区| 榆林市| 阆中市| 韶山市| 闸北区| 庄浪县| 宾川县| 达尔| 彩票| 河北省| 顺昌县| 北海市|