简体   繁体   English

jQuery:使用.after()或.before()仅将元素添加到选择中的最后一项

[英]jQuery: Using .after() or .before() adds element to last item in selection only

I've been using jQuery for a while but this is a new one. 我已经使用jQuery一段时间了,但这是一个新版本。 A simplified example: 一个简化的例子:

HTML 的HTML

<div class='custom'></div>
<div class='custom'></div>
<div class='custom'></div>

jQuery: jQuery的:

var $customElems = $('.custom'),
    $spanOuter   = $('<span class="outer"/>'),
    $spanInner   = $('<span class="inner"/>');

$customElems.each( function() {
    $(this).wrap($spanOuter).after($spanInner);
});

JSFiddle: http://jsfiddle.net/a3ZK8/ JSFiddle: http : //jsfiddle.net/a3ZK8/

I would have expected the 'inner' span to be added to all three elements in the selection but it gets always inserted into the last one only (no matter how many). 我本来希望将“内部”跨度添加到所选内容的所有三个元素中,但它总是总是插入到最后一个(无论有多少个)中。 I tried it with .before(), with and without the chaining, same result. 我使用.before()进行了尝试,无论是否进行链接,结果都是相同的。 What am I missing?? 我想念什么?

The problem is you are using a reference to a jQuery object. 问题是您正在使用对jQuery对象的引用

Hence you keep moving the object reference around within each iteration. 因此,您将在每次迭代中不断移动对象引用

If you have no events attached or no need for the span to be a jQuery object then just pass the parameter as a HTML string literal instead of an object reference 如果您没有附加事件或不需要将span用作jQuery对象,则只需将参数作为HTML字符串文字而不是对象引用进行传递

Cloning a jQuery object that doesn't need to be a jQuery object in the first place is just redundant processing and unnecessary overhead. 首先,克隆不需要是jQuery对象的jQuery对象只是多余的处理和不必要的开销。

Change your jQuery object to a string similar to this: 将您的jQuery对象更改为类似于以下内容的字符串:

spanInnerString  = '<span class="inner"/>';

and your method like this: 和你的方法是这样的:

$(this).wrap($spanOuter).after(spanInner);

The result is: 结果是:

<span class="outer"><div class="custom"></div><span class="inner"></span></span>
<span class="outer"><div class="custom"></div><span class="inner"></span></span>
<span class="outer"><div class="custom"></div><span class="inner"></span></span>

DEMO - Passing parameter as HTML string DEMO-将参数作为HTML字符串传递


Off course, the same goes for the outer span. 当然,外部跨度也一样。 Don't create jQuery objects unless you have to. 除非必须,否则不要创建jQuery对象。

If you must use a jQuery object because you want to attach events to the span or similar, than cloning is the way to go, though make sure you use clone(true, true) then to also clone the attached events. 如果必须将jQuery对象附加到span或类似对象上,则必须使用jQuery对象,而不是使用克隆方法,尽管请确保使用clone(true, true)然后也克隆附加的事件。

You need to clone the element. 您需要克隆元素。 Otherwise, after() will relocate the same element 3 times, which results in it being attached to only the last looped element. 否则, after()将重定位同一元素3次,从而导致该元素仅附加到最后一个循环的元素。

$customElems.each(function () {
    $(this).wrap($spanOuter).after($spanInner.clone());
});

Demo: Fiddle 演示: 小提琴

You might ask, "Why would wrap() work?" 您可能会问,“为什么wrap()起作用?” That's because 'wrap()' internally clones the element . 这是因为'wrap()'在内部克隆了element

You're moving the same span from place to place. 您要在同一位置之间移动同一跨度。 If you acted on all three divs at once, jquery will instead clone the span. 如果您一次对所有三个div进行操作,jquery将改为克隆范围。

http://jsfiddle.net/a3ZK8/1/ http://jsfiddle.net/a3ZK8/1/

var $customElems = $('.custom'),
    $spanOuter = $('<span class="outer"/>'),
    $spanInner = $('<span class="inner"/>');

$customElems.wrap($spanOuter).after($spanInner);

From the documentation for .after: 从.after的文档中:

Important: If there is more than one target element, cloned copies of the inserted element will be created for each target except for the last one. 重要提示:如果有多个目标元素,则将为每个目标(最后一个目标除外)创建插入元素的克隆副本。

which means the last element will always get the original, while all other selected elements will get a clone. 这意味着最后一个元素将始终获得原始元素,而所有其他选定元素将获得一个克隆。 That's why when you acted on one element at a time, it simply moved the same span around. 这就是为什么当您一次对一个元素进行操作时,它只会移动相同的跨度。

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

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