jquery?追加元素append、prepend、before、after用法與區(qū)別分析
功能說(shuō)明
對(duì)比的看
before和prepend
before:在被選元素之前添加的內(nèi)容(內(nèi)容外面)
prepend:在被選元素的前面添加的內(nèi)容(內(nèi)容的里面)
同理
after和append
after:在被選元素之后添加的內(nèi)容(內(nèi)容外面)
append:在被選元素的后面添加的內(nèi)容(內(nèi)容的里面)
jquery的插入外部
after() 元素外的后面插入
insertAfter() 把內(nèi)容插入到元素外的后面
before() 在元素外的前面插入內(nèi)容
insertBefore() 把內(nèi)容插入到目標(biāo)元素外的后面
詳細(xì)說(shuō)明
append與prepedn都是元素里面操作,這樣就比較簡(jiǎn)單了
一、after()和before()方法的區(qū)別
after()——其方法是將方法里面的參數(shù)添加到j(luò)query對(duì)象后面去;
如:A.after(B)的意思是將B放到A后面去;
before()——其方法是將方法里面的參數(shù)添加到j(luò)query對(duì)象前面去。
如:A.before(B)的意思是將A放到B前面去;
二、insertAfter()和insertBefore()的方法的區(qū)別
其實(shí)是將元素對(duì)調(diào)位置;
可以是頁(yè)面上已有元素;也可以是動(dòng)態(tài)添加進(jìn)來(lái)的元素。
如:A.insertAfter(B);即將A元素調(diào)換到B元素后面;
如<span>CC</span><p>HELLO</p>使用$("span").insertAfter($("p"))后,就變?yōu)?lt;p>HELLO</p><span>CC</span>了。兩者位置調(diào)換了
三、append()和appendTo()方法的區(qū)別
append()——其方法是將方法里面的參數(shù)添加到j(luò)query對(duì)象中來(lái);
如:A.append(B)的意思是將B放到A中來(lái),后面追加,A的子元素的最后一個(gè)位置;
appendTo()——其方法是將jquery對(duì)象添加到appendTo指定的參數(shù)中去。
如:A.appendTo(B)的意思是將A放到B中去,后面追加,B的子元素的最后一個(gè)位置;
四、prepend()和prependTo()方法的區(qū)別
append()——其方法是將方法里面的參數(shù)添加到j(luò)query對(duì)象中來(lái);
如:A.append(B)的意思是將B放到A中來(lái),插入到A的子元素的第一個(gè)位置;
appendTo()——其方法是將jquery對(duì)象添加到appendTo指定的參數(shù)中去。
如:A.appendTo(B)的意思是將A放到B中去,插入到B的子元素的第一個(gè)位置;
例子
1、insert局部方法
/**
* 在父級(jí)元素上操作DOM
* @param {Object} parent 父級(jí)元素,可以是element,也可以是Yquery對(duì)象
* @param {String} position 位置: beforebegin/afterbegin/beforeend/afterend
* @param {*} any 任何:string/text/object
* @param {Number} index 序號(hào),如果大于0則復(fù)制節(jié)點(diǎn)
* @return {Undefined}
* @version 1.0
* 2013年12月2日17:08:26
*/
function _insert(parent, position, any, index) {
if ($.isFunction(any)) {
any = any.call(parent);
}
// 字符串
if ($.isString(any)) {
if (regTag.test(any)) {
parent.insertAdjacentHTML(position, any);
} else {
parent.insertAdjacentText(position, any);
}
}
// 數(shù)字
else if ($.isNumber(any)) {
parent.insertAdjacentText(position, any);
}
// 元素
else if ($.isElement(any)) {
parent.insertAdjacentElement(position, index > 0 ? any.cloneNode(!0) : any);
}
// Yquery
else if (_isYquery(any)) {
any.each(function() {
_insert(parent, position, this);
});
}
}2、append、prepend、before、after
$.fn = {
/**
* 追插
* 將元素后插到當(dāng)前元素(集合)內(nèi)
* @param {String/Element/Function} any
* @return this
* @version 1.0
* 2013年12月29日1:44:15
*/
append: function(any) {
return this.each(function(index) {
_insert(this, 'beforeend', any, index);
});
},
/**
* 補(bǔ)插
* 將元素前插到當(dāng)前元素(集合)內(nèi)
* @param {String/Element/Function} any
* @return this
* @version 1.0
* 2013年12月29日1:44:15
*/
prepend: function(any) {
return this.each(function(index) {
_insert(this, 'afterbegin', any, index);
});
},
/**
* 前插
* 將元素前插到當(dāng)前元素(集合)前
* @param {String/Element/Function} any
* @return this
* @version 1.0
* 2013年12月29日1:44:15
*/
before: function(any) {
return this.each(function(index) {
_insert(this, 'beforebegin', any, index);
});
},
/**
* 后插
* 將元素后插到當(dāng)前元素(集合)后
* @param {String/Element/Function} any
* @return this
* @version 1.0
* 2013年12月29日1:44:15
*/
after: function(any) {
return this.each(function(index) {
_insert(this, 'afterend', any, index);
});
}
};3、prependTo、prependTo、insertBefore、insertAfter
這些帶后綴的與上面的不同的是,返回的結(jié)果不一樣。如:
$('#demo').append('<a/>');
// => 返回的是 $('#demo')
$('<a/>').appendTo($('#demo'));
// => 返回的是$('a');因此兩者的關(guān)系只是返回結(jié)果不一樣,其他的都一樣,可以這么解決:
_each({
appendTo: 'append',
prependTo: 'prepend',
insertBefore: 'before',
insertAfter: 'after'
}, function(key, val) {
$.fn[key] = function(selector) {
this.each(function() {
$(selector)[val](this);
});
return this;
};
});jquery 追加元素的方法(append prepend after before 的區(qū)別)
append() 方法在被選元素的結(jié)尾插入內(nèi)容。
prepend() 方法在被選元素的開(kāi)頭插入內(nèi)容。
after() 方法在被選元素之后插入內(nèi)容。
before() 方法在被選元素之前插入內(nèi)容。
<script type="text/javascript" src="http://common.jb51.net/jslib/jquery/jquery.min.js"></script>
<div class="testdiv">
<ul>
<li>第一個(gè)li標(biāo)簽</li>
</ul>
</div>
<script>
//append
$('.testdiv ul').append('<li>append 插入的li</li>');
//prepend
$('.testdiv ul').prepend('<li>prepend 插入的li</li>');
//after
$('.testdiv ul').after('<li>after 插入的li</li>');
//before
$('.testdiv ul').before('<li>before 插入的li</li>');
</script>效果圖

html結(jié)構(gòu)圖

- 解決jQuery使用append添加的元素事件無(wú)效的問(wèn)題
- jquery append 動(dòng)態(tài)添加的元素事件on 不起作用的解決方案
- jQuery 追加元素的方法如append、prepend、before
- jQuery中append、insertBefore、after與insertAfter的簡(jiǎn)單用法與注意事項(xiàng)
- jQuery使用before()和after()在元素前后添加內(nèi)容的方法
- jquery.ajax之beforeSend方法使用介紹
- jquery追加元素的所有方法全面深入實(shí)例講解(append、prepend、after、before、wrap等等)
相關(guān)文章
jquery實(shí)現(xiàn)的動(dòng)態(tài)回到頂部特效代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)的動(dòng)態(tài)回到頂部特效代碼,涉及jQuery基于時(shí)間函數(shù)的定時(shí)遞歸調(diào)用實(shí)現(xiàn)帶緩沖效果的移動(dòng)功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
jQuery取得設(shè)置清空select選擇的文本與值
這篇文章主要介紹了jQuery如何取得設(shè)置清空select選擇的文本與值,下面有個(gè)不錯(cuò)的示例,需要的朋友可以參考下2014-07-07
jquery實(shí)現(xiàn)簡(jiǎn)單的拖拽效果實(shí)例兼容所有主流瀏覽器
拖拽效果個(gè)人覺(jué)得是一種不錯(cuò)的用戶體驗(yàn),抽空研究了一下,原理還蠻簡(jiǎn)單的,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
關(guān)于jquery append() html時(shí)的小問(wèn)題的解決方法
關(guān)于jquery append() html時(shí)的小問(wèn)題,碰到類似問(wèn)題的朋友可以參考下。2010-12-12
jQuery實(shí)現(xiàn)頁(yè)面頂部顯示的進(jìn)度條效果完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)頁(yè)面頂部顯示的進(jìn)度條效果,以完整實(shí)例形式分析了jQuery基于animate與setTimeout定時(shí)觸發(fā)實(shí)現(xiàn)進(jìn)度條漸進(jìn)顯示功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12
基于jquery實(shí)現(xiàn)一個(gè)滾動(dòng)的分步注冊(cè)向?qū)?附源碼
使用jQuery實(shí)現(xiàn)很多很有意思的應(yīng)用效果。我們?cè)诤芏嗑W(wǎng)站注冊(cè)會(huì)員時(shí),需要填寫(xiě)注冊(cè)表單,包括登錄信息、個(gè)人聯(lián)系信息等,本文將帶您一起體驗(yàn)jQuery實(shí)現(xiàn)的一個(gè)可以滾動(dòng)的十分友好的分步注冊(cè)向?qū)?,需要的朋友可以參考?/div> 2015-08-08
JQuery Ajax跨域調(diào)用和非跨域調(diào)用問(wèn)題實(shí)例分析
這篇文章主要介紹了JQuery Ajax跨域調(diào)用和非跨域調(diào)用問(wèn)題,結(jié)合具體實(shí)例形式分析了jQuery基于ajax的非跨域請(qǐng)求及跨域請(qǐng)求相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-04-04
jQuery Easyui datagrid editor為combobox時(shí)指定數(shù)據(jù)源實(shí)例
當(dāng)在datagrid行內(nèi)部應(yīng)用添加編輯操作時(shí),引入combobox是非常方便的操作,這篇文章主要介紹了jQuery Easyui datagrid editor為combobox時(shí)指定數(shù)據(jù)源實(shí)例,有興趣的可以了解一下。2016-12-12
JQUERY實(shí)現(xiàn)網(wǎng)頁(yè)右下角固定位置展開(kāi)關(guān)閉特效的方法
這篇文章主要介紹了JQUERY實(shí)現(xiàn)網(wǎng)頁(yè)右下角固定位置展開(kāi)關(guān)閉特效的方法,涉及jquery操作頁(yè)面元素的顯示與隱藏等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07最新評(píng)論

