insertAfter()方法將指定的HTML元素插入到選定的元素之后。
要在選定元素之前插入HTML元素,請使用insertBefore()方法。
在after()和insertAfter()方法執(zhí)行相同的任務(wù)。主要區(qū)別在于語法:
使用after(),選擇表達(dá)式在函數(shù)的前面,參數(shù)是將要插入的內(nèi)容。
使用insertAfter(),剛好相反,內(nèi)容在方法前面,它將被放在參數(shù)里元素的后面。
$(content).insertAfter(selector)
在每個段落之后插入HTML元素:
$("button").click(function(){
$("<p style='color:red;'>Hello world</p>").insertAfter("p");
});測試看看?/?使用insertAfter()方法在每個選定元素之后插入一個現(xiàn)有元素:
$("button").click(function(){
$("h1").insertAfter("p");
});測試看看?/?| 參數(shù) | 描述 |
|---|---|
| content | 指定要插入的內(nèi)容(必須包含HTML標(biāo)記) 可能的值:
|
| selector | 所選元素將插入到此參數(shù)指定的元素之后 |