简体   繁体   English

。在插入多个元素之前

[英].before to insert multiple elements

I have an element var destination which I'm trying to insert multiple elements before it. 我有一个元素var destination ,我试图在它之前插入多个元素。 I use .before but I use it multiple times because I'm moving multiple elements. 我使用.before但是我多次使用它,因为我要移动多个元素。

Is there a way to improve this code, and move all the elements at once? 有没有一种方法可以改进此代码,并一次移动所有元素? I'm moving an element moveElement , the element before it, and a number of other elements that have the class myClass using each() . 我正在移动元素moveElement ,它之前的元素以及其他一些使用each()myClass的其他元素。 Any way to reduce the number of destination.before() 任何减少destination.before()数量的方法。

destination.before(moveElement.prev());
destination.before(moveElement);
$('div.myClass').each(function(){
   destination.before($(this));
});

before accepts a jquery object, so you can construct a jQuery object with all the elements. before接受一个jquery对象,因此您可以使用所有元素构造一个jQuery对象。

destination.before(moveElement.prev().add(moveElement).add('div.myClass'));

add docs: add文档:

Description: Add elements to the set of matched elements. 说明:将元素添加到匹配的元素集中。

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

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