ホームページ  >  記事  >  ウェブフロントエンド  >  jqueryのinsertBefore()、insertAfter()、prependTo()、appendTo()の使い方の詳しい説明

jqueryのinsertBefore()、insertAfter()、prependTo()、appendTo()の使い方の詳しい説明

巴扎黑
巴扎黑オリジナル
2017-06-24 13:19:201811ブラウズ

ナビゲーション:

1、insertBefore()、insertAfter()、prependTo()、appendTo()、これら 4 つの関数は使用方法がほぼ同じです

2。 () ,After(),prepend(),append()

1, jQuery.insertAfter() 詳しい関数説明(他の3つは使い方を参照)

insertAfter() 関数は、 現在一致するすべての要素を<strong>指定された要素の後に挿入</strong>するために使用されます。 <code>insertAfter()函数用于将当前所有匹配元素插入到指定元素之后

与该函数相对的是insertBefore()函数,用于将当前所有匹配元素插入到指定元素之前。

该函数属于jQuery对象(实例)。

语法

jQueryObject.insertAfter( target )

参数

参数 描述
target String/Element/jQuery类型指定的目标元素,以便于在该目标之后插入当前匹配的元素。

如果参数target为字符串类型,则将其视作jQuery选择器或html内容字符串,jQuery会自行判断。

返回值

insertAfter()函数的返回值jQuery类型,返回表示插入内容的jQuery对象。

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

示例&说明

insertAfter()函数用于将所有匹配元素插入到指定元素之后的位置:

<span class="tag" style="font-size: 18px;"><p><span class="pln">段落文本1<span class="tag"><span></span></p><span class="pln"><br/><span class="tag"><p><span class="pln">段落文本2<span class="tag"><span></span></p><span class="pln"><br/><br/><span class="com"><!--以上是jQuery代码执行前的html内容--><span class="pln"><br/><span class="tag"><script<span class="pln"> <span class="atn">type<span class="pun">=<span class="atv">"text/<a href="http://www.php.cn/wiki/48.html" target="_blank">javascript</a>"<span class="tag">><span class="pln"><br/>$<span class="pun">(<span class="str">&#39;<!--插入到p元素之后的位置-->&#39;<span class="pun">).<span class="pln">insertAfter<span class="pun">(<span class="pln"> <span class="str">"p"<span class="pln"> <span class="pun">);<span class="pln"><br/><span class="com">// 其返回值就是匹配插入内容(两个注释元素&#39;<!--插入到p元素之后的位置-->&#39;)的jQuery对象<span class="pln"><br/><span class="tag"></script><span class="pln"><br/><span class="com"><!--以下是jQuery代码执行后的html内容--><span class="pln"><br/><br/><span class="tag"><p><span class="pln">段落文本1<span class="tag"><span></span></p><span class="com"><!--插入到p元素之后的位置--><span class="pln"><br/><span class="tag"><p><span class="pln">段落文本2<span class="tag"><span></span></p><span class="com"><!--插入到p元素之后的位置--></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

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

<span class="kwd" style="font-size: 18px; color: #0000ff;">var<span class="pln"> $A <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"s1"<span class="pun">);<span class="pln"><br/><span class="kwd">var<span class="pln"> $B <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"s2"<span class="pun">);<span class="pln"><br/><br/><span class="com">// 将$A插入到$B之后<span class="pln"><br/>$A<span class="pun">.<span class="pln">insertAfter<span class="pun">(<span class="pln"> $B <span class="pun">);<span class="pln"> <span class="com">// 返回表示插入内容的jQuery对象( 匹配所有$B之后插入的$A元素 )<span class="pln"><br/><span class="com">// 将$B插入到$A之后<span class="pln"><br/>$A<span class="pun">.<span class="pln">after<span class="pun">(<span class="pln"> $B <span class="pun">);<span class="pln"> <span class="com">// 返回$A</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

以下面这段HTML代码为例:

<span class="tag" style="font-size: 18px;"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n1"<span class="tag">><span class="pln"><br/>    <span class="tag"><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n2"<span class="tag">><span class="pln">span#n2<span class="tag"></span><span class="pln">    <br/><span class="tag"></p><span class="pln"><br/><span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n3"<span class="tag">><span class="pln"><br/>    <span class="tag"><label<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n4"<span class="pln"> <span class="atn">class<span class="pun">=<span class="atv">"move"<span class="tag">><span class="pln">label#n4<span class="tag"></label><span class="pln"><br/><span class="tag"></p><span class="pln"><br/><span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n5"<span class="tag">><span class="pln"><br/>    <span class="tag"><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n6"<span class="tag">><span class="pln">span#n6<span class="tag"></span><span class="pln"><br/><span class="tag"></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

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

<span class="com" style="font-size: 18px;">// 将一个自定义的i元素插入到n4之后<span class="pln"><br/>$<span class="pun">(<span class="str">&#39;<i>i元素</i>&#39;<span class="pun">).<span class="pln">insertAfter<span class="pun">(<span class="pln"> <span class="str">"#n4"<span class="pln"> <span class="pun">);<span class="pln"><br/><br/><span class="com">// 将n4插入到n2之后<span class="pln"><br/><span class="com">// n4将从原位置上消失<span class="pln"><br/>$<span class="pun">(<span class="str">&#39;#4&#39;<span class="pun">).<span class="pln">insertAfter<span class="pun">(<span class="pln"> document<span class="pun">.<span class="pln">getElementById<span class="pun">(<span class="str">"n2"<span class="pun">)<span class="pln"> <span class="pun">);<span class="pln"><br/><br/><span class="com">//将一个自定义的strong元素插入到每个span元素之后<span class="pln"><br/>$<span class="pun">(<span class="str">"<strong>插入文本</strong>"<span class="pun">).<span class="pln">insertAfter<span class="pun">(<span class="pln"> <span class="str">"span"<span class="pln"> <span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

运行代码

insertAfter()会将当前匹配元素插入到目标元素的结束标记之后不会额外添加任何空白字符,上述代码执行后的完整html代码如下(格式未作任何调整):

<span class="tag" style="font-size: 18px;"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n1"<span class="tag">><span class="pln"><br/>    <span class="tag"><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n2"<span class="tag">><span class="pln">span#n2<span class="tag"></span><strong><span class="pln">插入文本<span class="tag"></strong><span class="pln">    <br/><span class="tag"></p><span class="pln"><br/><span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n3"<span class="tag">><span class="pln"><br/>    <span class="tag"><label<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n4"<span class="pln"> <span class="atn">class<span class="pun">=<span class="atv">"move"<span class="tag">><span class="pln">label#n4<span class="tag"></label><i><span class="pln">i元素<span class="tag"></i><span class="pln"><br/><span class="tag"></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

 

2,after()函数用法详解(其他三个可参考其用法)

 

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

指定的内容可以是:html字符串、DOM元素(或数组)、jQuery对象、函数(返回值)。

与该函数相对的是before()函数,用于在每个匹配元素之前插入指定的内容。

该函数属于jQuery

この関数に関連するのは、insertBefore() 関数です。この関数は、指定された要素 の前に、現在一致するすべての要素を挿入するために使用されます。

この関数は jQuery オブジェクト (インスタンス) に属します。

構文パラメータ説明ターゲットパラメータtargetが文字列型の場合、jQueryセレクタまたはhtmlコンテンツ文字列とみなされ、jQueryが独自に判断します。 戻り値insertAfter()戻り値function 値 jQuery タイプで、挿入されたコンテンツを表す jQuery オブジェクトを返します。
jQueryObject.after( content1 [, content2 [, contentN ]] )
パラメータ
ターゲット要素は String/Element/jQuery タイプで指定され、現在一致する要素をターゲットの後に挿入できます。
🎜

🎜注: 現在の jQuery オブジェクトと一致する要素が現在のページ内の特定の要素である場合、これらの要素は元の位置から消えます。つまり、これはコピー操作ではなく、移動操作に相当します。 🎜🎜🎜🎜🎜例と説明🎜🎜🎜🎜insertAfter() 関数は、一致するすべての要素を指定された要素 の後の位置に挿入するために使用されます: 🎜🎜🎜

<span class="tag" style="font-size: 14pt;"><p><span class="pln">段落文本1<span class="tag"><span></span></p><span class="com"><!--插入到p元素之后的位置--><span class="pln"><br/><span class="tag"><p><span class="pln">段落文本2<span class="tag"><span></span></p><span class="com"><!--插入到p元素之后的位置--><span class="pln"><br/><br/><br/><span class="tag"><script<span class="pln"> <span class="atn">type<span class="pun">=<span class="atv">"text/javascript"<span class="tag">><span class="pln"><br/>$<span class="pun">(<span class="str">"p"<span class="pun">).<span class="pln">after<span class="pun">(<span class="pln"> <span class="str">&#39;<!--插入到p元素之后的位置-->&#39;<span class="pln"> <span class="pun">);<span class="pln"> <br/><span class="tag"></script></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜 insertAfter() 関数と アフターに注意してください。 ()🎜 関数の違い: 🎜🎜
<span class="kwd" style="font-size: 14pt;">var<span class="pln"> $A <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"s1"<span class="pun">);<span class="pln"><br/><span class="kwd">var<span class="pln"> $B <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"s2"<span class="pun">);<span class="pln"><br/><br/><br/><span class="com">// 将$B插入到$A之后<span class="pln"><br/>$A<span class="pun">.<span class="pln">after<span class="pun">(<span class="pln"> $B <span class="pun">);<span class="pln"> <span class="com">// 返回$A<span class="pln"><br/><span class="com">// 将$A插入到$B之后<span class="pln"><br/>$A<span class="pun">.<span class="pln">insertAfter<span class="pun">(<span class="pln"> $B <span class="pun">);<span class="pln"> <span class="com">// 返回表示插入内容的jQuery对象( 匹配所有$B之后插入的$A元素 )</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜 次の HTML コードを例に挙げます。 🎜🎜
<span class="tag" style="font-size: 14pt;"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n1"<span class="tag">><span class="pln"><br/>    <span class="tag"><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n2"<span class="tag">><span class="pln">span#n2<span class="tag"></span><span class="pln">    <br/><span class="tag"></p><span class="pln"><br/><span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n3"<span class="tag">><span class="pln"><br/>    <span class="tag"><label<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n4"<span class="tag">><span class="pln">label#n4<span class="tag"></label><span class="pln"><br/>    <span class="tag"><i<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n5"<span class="tag">><span class="pln">i#n5<span class="tag"></i><span class="pln"><br/><span class="tag"></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜 次の jQuery サンプル コードは、insertAfter() 関数の具体的な使用法を示すために使用されます。 : 🎜🎜
<span class="com" style="font-size: 14pt;">// 在n4之后插入一个自定义的span元素<span class="pln"><br/>$<span class="pun">(<span class="str">"#n4"<span class="pun">).<span class="pln">after<span class="pun">(<span class="str">&#39;<span id="n6">span#n6(new)</span>&#39;<span class="pun">);<span class="pln"><br/><br/><span class="com">// 在n2之后插入n5<span class="pln"><br/><span class="com">// n5将从原位置上消失<span class="pln"><br/>$<span class="pun">(<span class="str">"#n2"<span class="pun">).<span class="pln">after<span class="pun">(<span class="pln"> document<span class="pun">.<span class="pln">getElementById<span class="pun">(<span class="str">"n5"<span class="pun">)<span class="pln"> <span class="pun">);<span class="pln"><br/><br/><span class="com">// 在每个span元素之后插入自定义的strong元素<span class="pln"><br/>$<span class="pun">(<span class="str">"span"<span class="pun">).<span class="pln">after<span class="pun">(<span class="pln"> <span class="kwd">function<span class="pun">(<span class="pln">index<span class="pun">,<span class="pln"> innerHTML<span class="pun">){<span class="pln"><br/>    <span class="kwd">return<span class="pln"> <span class="str">&#39;<strong>strong元素&#39;<span class="pln"> <span class="pun">+<span class="pln"> <span class="pun">(<span class="pln">index <span class="pun">+<span class="pln"> <span class="lit">1<span class="pun">)<span class="pln"> <span class="pun">+<span class="pln"> <span class="str">&#39;</strong>&#39;<span class="pun">;<span class="pln"><br/><span class="pun">}<span class="pln"> <span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜コードを実行します🎜🎜🎜🎜insertAfter() は、終了タグの後に現在の一致する要素をターゲット要素に挿入しますが、挿入しません追加の空白文字を追加します。上記のコードを実行した後の完全な HTML コードは次のとおりです (形式は調整されていません): 🎜🎜
<span class="tag" style="font-size: 14pt;"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n1"<span class="tag">><span class="pln"><br/>    <span class="tag"><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n2"<span class="tag">><span class="pln">span#n2<span class="tag"></span><strong><span class="pln">strong元素1<span class="tag"></strong><i<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n5"<span class="tag">><span class="pln">i#n5<span class="tag"></i><span class="pln">    <br/><span class="tag"></p><span class="pln"><br/><span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n3"<span class="tag">><span class="pln"><br/>    <span class="tag"><label<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n4"<span class="tag">><span class="pln">label#n4<span class="tag"></label><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n6"<span class="tag">><span class="pln">span#n6(new)<span class="tag"></span><strong><span class="pln">strong元素2<span class="tag"></strong><span class="pln"><br/>    <br/><span class="tag"></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜🎜🎜🎜2. 使用法の詳細な説明。 after() 関数の🎜 🎜🎜🎜 🎜🎜🎜 after() 関数は、 各関数の後に指定されたコンテンツを挿入するために使用されます。一致する要素です。 🎜🎜🎜🎜指定できる内容は、html文字列、DOM要素(または配列)、jQueryオブジェクト、関数(戻り値)です。 🎜🎜🎜🎜この関数に関連するのは before() 関数です。この関数は、一致する各要素 の前に指定されたコンテンツを挿入するために使用されます。 🎜🎜🎜🎜🎜この関数は jQuery オブジェクト (インスタンス) に属します。 🎜🎜🎜🎜構文🎜🎜rrreee🎜🎜パラメータ🎜🎜🎜🎜🎜🎜パラメータ🎜🎜🎜🎜説明🎜🎜🎜🎜🎜🎜content1🎜🎜 🎜🎜文字列/​​要素/jQuery/関数タイプで指定された追加コンテンツ。 🎜🎜🎜🎜🎜🎜content2🎜🎜🎜🎜Optional/String/Element/jQuery型で指定された追加コンテンツ。 🎜🎜🎜🎜🎜🎜contentN🎜🎜🎜🎜Optional/String/Element/jQueryタイプで指定された追加コンテンツは、任意の数があります。 🎜🎜🎜🎜🎜

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

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

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

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

返回值

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

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

示例&说明

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

<span class="tag" style="font-size: 14pt;"><p><span class="pln">段落文本1<span class="tag"><span></span></p><span class="com"><!--插入到p元素之后的位置--><span class="pln"><br/><span class="tag"><p><span class="pln">段落文本2<span class="tag"><span></span></p><span class="com"><!--插入到p元素之后的位置--><span class="pln"><br/><br/><br/><span class="tag"><script<span class="pln"> <span class="atn">type<span class="pun">=<span class="atv">"text/javascript"<span class="tag">><span class="pln"><br/>$<span class="pun">(<span class="str">"p"<span class="pun">).<span class="pln">after<span class="pun">(<span class="pln"> <span class="str">&#39;<!--插入到p元素之后的位置-->&#39;<span class="pln"> <span class="pun">);<span class="pln"> <br/><span class="tag"></script></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

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

<span class="kwd" style="font-size: 14pt;">var<span class="pln"> $A <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"s1"<span class="pun">);<span class="pln"><br/><span class="kwd">var<span class="pln"> $B <span class="pun">=<span class="pln"> $<span class="pun">(<span class="str">"s2"<span class="pun">);<span class="pln"><br/><br/><br/><span class="com">// 将$B插入到$A之后<span class="pln"><br/>$A<span class="pun">.<span class="pln">after<span class="pun">(<span class="pln"> $B <span class="pun">);<span class="pln"> <span class="com">// 返回$A<span class="pln"><br/><span class="com">// 将$A插入到$B之后<span class="pln"><br/>$A<span class="pun">.<span class="pln">insertAfter<span class="pun">(<span class="pln"> $B <span class="pun">);<span class="pln"> <span class="com">// 返回表示插入内容的jQuery对象( 匹配所有$B之后插入的$A元素 )</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

以下面这段HTML代码为例:

<span class="tag" style="font-size: 14pt;"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n1"<span class="tag">><span class="pln"><br/>    <span class="tag"><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n2"<span class="tag">><span class="pln">span#n2<span class="tag"></span><span class="pln">    <br/><span class="tag"></p><span class="pln"><br/><span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n3"<span class="tag">><span class="pln"><br/>    <span class="tag"><label<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n4"<span class="tag">><span class="pln">label#n4<span class="tag"></label><span class="pln"><br/>    <span class="tag"><i<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n5"<span class="tag">><span class="pln">i#n5<span class="tag"></i><span class="pln"><br/><span class="tag"></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

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

<span class="com" style="font-size: 14pt;">// 在n4之后插入一个自定义的span元素<span class="pln"><br/>$<span class="pun">(<span class="str">"#n4"<span class="pun">).<span class="pln">after<span class="pun">(<span class="str">&#39;<span id="n6">span#n6(new)</span>&#39;<span class="pun">);<span class="pln"><br/><br/><span class="com">// 在n2之后插入n5<span class="pln"><br/><span class="com">// n5将从原位置上消失<span class="pln"><br/>$<span class="pun">(<span class="str">"#n2"<span class="pun">).<span class="pln">after<span class="pun">(<span class="pln"> document<span class="pun">.<span class="pln">getElementById<span class="pun">(<span class="str">"n5"<span class="pun">)<span class="pln"> <span class="pun">);<span class="pln"><br/><br/><span class="com">// 在每个span元素之后插入自定义的strong元素<span class="pln"><br/>$<span class="pun">(<span class="str">"span"<span class="pun">).<span class="pln">after<span class="pun">(<span class="pln"> <span class="kwd">function<span class="pun">(<span class="pln">index<span class="pun">,<span class="pln"> innerHTML<span class="pun">){<span class="pln"><br/>    <span class="kwd">return<span class="pln"> <span class="str">&#39;<strong>strong元素&#39;<span class="pln"> <span class="pun">+<span class="pln"> <span class="pun">(<span class="pln">index <span class="pun">+<span class="pln"> <span class="lit">1<span class="pun">)<span class="pln"> <span class="pun">+<span class="pln"> <span class="str">&#39;</strong>&#39;<span class="pun">;<span class="pln"><br/><span class="pun">}<span class="pln"> <span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

运行代码

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

<span class="tag" style="font-size: 14pt;"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n1"<span class="tag">><span class="pln"><br/>    <span class="tag"><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n2"<span class="tag">><span class="pln">span#n2<span class="tag"></span><strong><span class="pln">strong元素1<span class="tag"></strong><i<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n5"<span class="tag">><span class="pln">i#n5<span class="tag"></i><span class="pln">    <br/><span class="tag"></p><span class="pln"><br/><span class="tag"><p<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n3"<span class="tag">><span class="pln"><br/>    <span class="tag"><label<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n4"<span class="tag">><span class="pln">label#n4<span class="tag"></label><span<span class="pln"> <span class="atn">id<span class="pun">=<span class="atv">"n6"<span class="tag">><span class="pln">span#n6(new)<span class="tag"></span><strong><span class="pln">strong元素2<span class="tag"></strong><span class="pln"><br/>    <br/><span class="tag"></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

以上がjqueryのinsertBefore()、insertAfter()、prependTo()、appendTo()の使い方の詳しい説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。