Home  >  Article  >  Web Front-end  >  Detailed explanation of jquery's insertBefore(), insertAfter(), prependTo(), appendTo() usage

Detailed explanation of jquery's insertBefore(), insertAfter(), prependTo(), appendTo() usage

巴扎黑
巴扎黑Original
2017-06-24 13:19:201627browse

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()

1, jQuery.insertAfter() Detailed explanation of function(The other three refer to their usage)

##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 the

jQuery object (instance).

Syntax

jQueryObject.insertAfter( target )

Parameters

ParameterDescriptiontargetIf the parameter
##String/Element/jQuery type specification The target element so that the currently matched element is inserted after the target.
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

insertAfter()

The return value of the functionisjQuery 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

disappear

from their original positions. In short, this is equivalent to a move operation, not a copy operation. Example & Description

insertAfter()

function is used to insert all matching elements into the specified element Position after :

段落文本1


段落文本2




type="text/javascript">
$('').insertAfter( "p" );
// 其返回值就是匹配插入内容(两个注释元素'')的jQuery对象



段落文本1


段落文本2

Please note the difference between the

insertAfter()

function and the after() function:

var $A = $("s1");
var $B = $("s2");

// 将$A插入到$B之后
$A.insertAfter( $B ); // 返回表示插入内容的jQuery对象( 匹配所有$B之后插入的$A元素 )
// 将$B插入到$A之后
$A.after( $B ); // 返回$A
Take the following HTML code as an example:

 id="n1">
id="n2">span#n2


id="n3">
id="n4" class="move">label#n4


id="n5">
id="n6">span#n6

The following jQuery sample code is used to demonstrate

insertAfter()

Specific usage of the function:

// 将一个自定义的i元素插入到n4之后
$('i元素').insertAfter( "#n4" );

// 将n4插入到n2之后
// n4将从原位置上消失
$('#4').insertAfter( document.getElementById("n2") );

//将一个自定义的strong元素插入到每个span元素之后
$("插入文本").insertAfter( "span" );
Run the code

##insertAfter()

will insert the current matching element into the target element After the

closing tag , 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="n1">
id="n2">span#n2插入文本


id="n3">
id="n4" class="move">label#n4i元素

2, detailed explanation of the usage of after() function (the other three can refer to their usage)

after()

function is used to

insert 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 ]] )

Parameters

ParameterString/Element/jQuery/Function Additional content specified by type. Optional/String/Element/jQuery type specified additional content. Optional/String/Element/jQuery type specified additional content, there can be any number .
Description ##content1
content2
contentN

after()可以将多个参数所表示的内容全部插入到紧邻每个匹配元素之后的位置。如果参数为字符串类型,则将其视作html字符串。

jQuery 1.4 新增支持:参数content1可以为函数。after()将根据匹配的所有元素遍历执行该函数,函数中的this将指向对应的DOM元素。

after()还会为函数传入两个参数:第一个参数就是当前元素在匹配元素中的索引,第二个参数就是该元素当前的内部html内容(innerHTML)。函数的返回值就是需要插入的内容(可以是html字符串、DOM元素、jQuery对象)。

注意:只有第一个参数可以为自定义函数,用于遍历执行。如果之后的参数也为函数,则调用其toString()方法,将其转为字符串,并视为html内容。

返回值

after()函数的返回值为jQuery类型,返回当前jQuery对象本身(以便于进行链式风格的编程)。

注意:如果插入的内容是当前页面中的某些元素,那么这些元素将从原位置上消失。简而言之,这相当于一个移动操作,而不是复制操作。

示例&说明

after()函数用于在每个匹配元素之后的位置插入内容:

段落文本1


段落文本2




type="text/javascript">
$("p").after( '' );

请注意after()函数和insertAfter()函数的区别:

var $A = $("s1");
var $B = $("s2");


// 将$B插入到$A之后
$A.after( $B ); // 返回$A
// 将$A插入到$B之后
$A.insertAfter( $B ); // 返回表示插入内容的jQuery对象( 匹配所有$B之后插入的$A元素 )

以下面这段HTML代码为例:

 id="n1">
id="n2">span#n2


id="n3">
id="n4">label#n4
id="n5">i#n5

以下jQuery示例代码用于演示after()函数的具体用法:

// 在n4之后插入一个自定义的span元素
$("#n4").after('span#n6(new)');

// 在n2之后插入n5
// n5将从原位置上消失
$("#n2").after( document.getElementById("n5") );

// 在每个span元素之后插入自定义的strong元素
$("span").after( function(index, innerHTML){
return 'strong元素' + (index + 1) + '';
} );

运行代码

after()会将内容插入指定元素的结束标记之后不会额外添加任何空白字符,上述代码执行后的完整html代码如下(格式未作任何调整):

 id="n1">
id="n2">span#n2strong元素1 id="n5">i#n5


id="n3">
id="n4">label#n4 id="n6">span#n6(new)strong元素2

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn