简体   繁体   English

Jquery after()方法删除DOM元素

[英]Jquery after() method removing DOM element

The Jquery after() method is used to insert HTML element after the selected elements. Jquery after()方法用于在所选元素之后插入HTML元素。 My code is 我的代码是

  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
  </div>

Jquery Code: Jquery代码:

$( ".inner" ).after( "<h2>Greetings</h2>" );

The above code working fine. 上面的代码工作正常。 The Jquery is inserting <h2>Greetings</h2> code after two div tags. Jquery在两个div标签之后插入<h2>Greetings</h2>代码。 The HTML generating like below HTML生成如下

<div class="container">
  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <h2>Greetings</h2>
  <div class="inner">Goodbye</div>
  <h2>Greetings</h2>
</div>

But when i pass Jquery selector $('h2') as a parameter to after() method like below 但是当我将Jquery选择器$('h2')作为参数传递给after()方法时,如下所示

$( ".inner" ).after( $('h2') );

Jquery removes the first original <h2>Greetings</h2> tag and after it inserts <h2>Greetings</h2> tag after two div tags. Jquery删除第一个原始<h2>Greetings</h2>标记,然后在两个div标记后插入<h2>Greetings</h2>标记。 The HTML generating like below. HTML生成如下。

<div class="container">
  <div class="inner">Hello</div>
  <h2>Greetings</h2>
  <div class="inner">Goodbye</div>
  <h2>Greetings</h2>
</div>

What is the difference when pass Jquery DOM selector and plain HTML tag. 传递Jquery DOM选择器和纯HTML标记时有什么区别。

When you use a string, jQuery creates the neccessary elements based on the string of HTML passed in, so 当你使用一个字符串时,jQuery会根据传入的HTML字符串创建必要的元素,所以

$("<h2>Greetings</h2>")

actually creates a new H2 element, while 实际上创建了一个新的H2元素

$('h2')

selects all H2 elements in the document. 选择文档中的所有H2元素。

As after() does something like 因为after()做了类似的事情

 $.fn.after = function(arg) {
     return this.each(function() { // loops over all .inner elements

         var elem = $(arg); // "h2" selects all H2, "<h2>" creates H2 elements
         this.parentNode.insertBefore(elem, this.nextSibling); // something like that,
     });                                                       // not really important ?
 }

..it either creates a new H2 ( $("<h2>") ) for every matching element found in $( ".inner" ) , because of the each loop, or it just moves the same elements from $('h2') to after each .inner , meaning the selected H2 elements will end up after the last .inner ..或者为$( ".inner" )找到的每个匹配元素创建一个新的H2 $("<h2>") ,因为each循环,或者只是从$('h2')移动相同的元素$('h2')到每个.inner ,意味着所选的H2元素将在最后一个.inner之后结束

When you use a selector, you select the existing H2 element that is on the page, and move them 使用选择器时,选择页面上现有的H2元素并移动它们

var h2 = $('h2'); // selects all H2 elements on the page

$( ".inner" ).after( h2 ); // moves those elements after .inner

When you use $("selector") you're referencing the DOM element, in your example you're selecting the "h2" element. 当您使用$(“selector”)时,您正在引用DOM元素,在您的示例中,您正在选择“h2”元素。

So when you append it to another element or using after(), you're taking the element itself and move it to its new place. 因此,当您将其附加到另一个元素或使用after()时,您将自己获取元素并将其移动到新位置。

You can use JQuery clone function to clone the selected element 您可以使用JQuery 克隆功能来克隆所选元素

var clonedH2 = $("h2").clone()
$("selector").after(clonedH2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM