Home > Article > Web Front-end > Detailed explanation of jquery's insertBefore(), insertAfter(), prependTo(), appendTo() usage
Navigation:
1, insertBefore(), insertAfter(), prependTo(), appendTo() these four functions are almost the same in usage
2, In contrast, there are four functions: Before(),After(),prepend(),append()
##insertAfter() The function is used to
insert all current matching elements after the specified element.
Relative to this function is the insertBefore() function, which is used to insert all current matching elements before the specified element .
This function belongs to thejQuery object (instance).
jQueryObject.insertAfter( target )
Description | |
---|---|
##String/Element/jQuery type specification The target element so that the currently matched element is inserted after the target. |
is a string type, it is treated as a jQuery selector or html content string, jQuery I will make my own judgment. Return value
The return value of the functionis
jQuery Type that returns a jQuery object representing the inserted content. Note: If the elements matched by the current jQuery object are certain elements in the current page, then these elements will
from their original positions. In short, this is equivalent to a move operation, not a copy operation. Example & Description
function is used to insert all matching elements into the specified element Position after : 段落文本1 段落文本2 段落文本1 段落文本2
Please note the difference between the
function and the after() function:
var $A = $("s1");
Take the following HTML code as an example:
var $B = $("s2");
// 将$A插入到$B之后
$A.insertAfter( $B ); // 返回表示插入内容的jQuery对象( 匹配所有$B之后插入的$A元素 )
// 将$B插入到$A之后
$A.after( $B ); // 返回$A
id="n1">
id="n2">span#n2
id="n3">
id="n5">
id="n6">span#n6
The following jQuery sample code is used to demonstrate insertAfter() Specific usage of the function:
Run the code// 将一个自定义的i元素插入到n4之后
$('i元素').insertAfter( "#n4" );
// 将n4插入到n2之后
// n4将从原位置上消失
$('#4').insertAfter( document.getElementById("n2") );
//将一个自定义的strong元素插入到每个span元素之后
$("插入文本").insertAfter( "span" );
##insertAfter()
will insert the current matching element into the target element After the closing tag , id="n1"> id="n3"> will not add any additional whitespace characters
. The complete html code after the above code is executed is as follows (no adjustment to the format):
id="n2">span#n2插入文本
i元素
after()
function is used toinsert specified content after each matching element. The specified content can be: html string, DOM element (or array), jQuery object, function (return value).
Relative to this function is the before() function, which is used to insert specified content before each matching element
.This function belongs to the jQuery object (instance).
Syntax
jQueryObject.after( content1 [, content2 [, contentN ]] )
Description | ##content1 |
---|---|
content2 | |
contentN | |
jQuery 1.4 新增支持:参数
注意:只有第一个参数可以为自定义函数,用于遍历执行。如果之后的参数也为函数,则调用其toString()方法,将其转为字符串,并视为html内容。 返回值
注意:如果插入的内容是当前页面中的某些元素,那么这些元素将从原位置上消失。简而言之,这相当于一个移动操作,而不是复制操作。 示例&说明
请注意 var $A = $("s1"); 以下面这段HTML代码为例:
以下jQuery示例代码用于演示 // 在n4之后插入一个自定义的span元素 运行代码
|
The above is the detailed content of Detailed explanation of jquery's insertBefore(), insertAfter(), prependTo(), appendTo() usage. For more information, please follow other related articles on the PHP Chinese website!