replaceWith()
用提供的內(nèi)容替換集合中所有匹配的元素并且返回被刪除元素的集合。
.replaceWith()可以從DOM中移除內(nèi)容,然后在這個(gè)地方插入新的內(nèi)容。
通過調(diào)用replaceChild處理即可,但是這里需要注意的問題,就是事件的處理,因?yàn)楣?jié)點(diǎn)的變更所以涉及到要移除這個(gè)節(jié)點(diǎn)的數(shù)據(jù)cleanData方法。
replaceWith: function() { var arg = arguments[0]; this.domManip(arguments, function(elem) { arg = this.parentNode; jQuery.cleanData(getAll(this)); if (arg) { arg.replaceChild(elem, this); } }); return arg && (arg.length || arg.nodeType) ? this : this.remove(); }
.replaceWith()方法,和大部分其他jQuery方法一樣,返回jQuery對(duì)象,所以可以和其他方法鏈接使用,但是需要注意的是:對(duì)于該方法而言,該對(duì)象指向已經(jīng)從 DOM 中被移除的對(duì)象,而不是指向替換用的對(duì)象。
刪除目標(biāo)節(jié)點(diǎn)
jQuery( this ).remove();
然后再插入一個(gè)新節(jié)點(diǎn)
parent.insertBefore( elem, next );
將匹配元素集合從DOM中刪除要涉及到empty,remove,detach方法。
請(qǐng)驗(yàn)證,完成請(qǐng)求
由于請(qǐng)求次數(shù)過多,請(qǐng)先驗(yàn)證,完成再次請(qǐng)求
打開微信掃碼自動(dòng)綁定
綁定后可得到
使用 Ctrl+D 可將課程添加到書簽
舉報(bào)