简体   繁体   English

用JavaScript重新排列一系列生成的div

[英]re arranging a series of generated divs with javascript

I have a series of generated products which somewhat looks like this 我有一系列生成的产品,看起来像这样

<div class="product"><div class="productImage" title="one"></div></div>
<div class="product"><div class="productImage" title="two"></div></div>
<div class="product"><div class="productImage" title="three"></div></div>
<div class="product"><div class="productImage" title="four"></div></div>

Is there a way I could put them in an array to show them in different order using javascript? 有没有一种方法可以将它们放在数组中,以使用javascript以不同的顺序显示它们?

Cant figure it out. 无法弄清楚。 Any suggestion? 有什么建议吗?

what I have right now
<div class="product">
    <div class="productImage" title="Elixer Altima">product image goes here with other shopping features</div>
</div>
<div class="product">
    <div class="productImage" title="Bain Oleo Relax">product image goes here with other shopping features</div>
</div>
<div class="product">
    <div class="productImage" title="Satin">product image goes here with other shopping features</div>
</div>
<div class="product">
    <div class="productImage" title="Hair Cream">product image goes here with other shopping features</div>
</div>

New Order to be
<div class="product">
    <div class="productImage" title="Hair Cream">product image goes here with other shopping features</div>
</div>
<div class="product">
    <div class="productImage" title="Satin">product image goes here with other shopping features</div>
</div>
<div class="product">
    <div class="productImage" title="Bain Oleo Relax"></div>
</div>
<div class="product">
    <div class="productImage" title="Elixer Altima">product image goes here with other shopping features</div>
</div>

Assume you want titles in alphabetic order . 假设您要按字母顺序排列标题。

var $items= $('.product').detach();

$('#container').append( $items.sort( sortTitles));


function sortTitles(a,b){
    return a.title > b.title ? 1 :-1;

}

Adjust sort function for other attributes as required 根据需要调整其他属性的排序功能

You can sort based on the text, html, attributes and more if you like. 您可以根据需要对文本,html,属性等进行排序。 For instnace, if you wanted to sort based on the alphabetical order of the titles themselves, the following would work (I've added a #products parent element for demonstration purposes): 为了方便起见,如果您想根据标题本身的字母顺序进行排序,则可以进行以下操作(为演示起见,我添加了一个#products父元素):

$("#products .product").sort(function(a,b){
    var _a = $(a).text(), _b = $(b).text();
    return _a > _b ? 1 : -1;
})​.appendTo("#products");​​​​

Fiddle: http://jsfiddle.net/yxCWd/ 小提琴: http//jsfiddle.net/yxCWd/

In your case, it appears you just want to reverse the natural order. 在您的情况下,您似乎只想颠倒自然顺序。 This could be done any number of ways. 这可以通过许多方法来完成。 One such way would be to use the .reverse method on the Array prototype to reverse the order of a collection, and then re-append the elements to their parent: 一种方法是在Array原型上使用.reverse方法来反转集合的顺序,然后将元素重新附加到其父级:

var container = $("#products");
container.append(container.find(".product").get().reverse());​

Calling .get converts our collection to an array, thus giving us the ability to call .reverse . 调用.get将我们的集合转换为数组,从而使我们能够调用.reverse

Fiddle: http://jsfiddle.net/yxCWd/2/ 小提琴: http : //jsfiddle.net/yxCWd/2/

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

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