admin管理员组

文章数量:1314442

how i can make the ".each" to starts with the div id small number "1" to the big number "5" ... 1 / 2 / 3 / 4 / 5

lets say i have this divs

<div class="TID_5">TID 5</div>
<div class="TID_4">TID 4</div>
<div class="TID_3">TID 3</div>
<div class="TID_2">TID 2</div>
<div class="TID_1">TID 1</div>

i have this jquery what im using, but starts with the first div class id number "5" but i need to start with number 1 ...

$("div[class*='TID_']").each(function() {
 // code is e here ...
});

how i can make the ".each" to starts with the div id small number "1" to the big number "5" ... 1 / 2 / 3 / 4 / 5

lets say i have this divs

<div class="TID_5">TID 5</div>
<div class="TID_4">TID 4</div>
<div class="TID_3">TID 3</div>
<div class="TID_2">TID 2</div>
<div class="TID_1">TID 1</div>

i have this jquery what im using, but starts with the first div class id number "5" but i need to start with number 1 ...

$("div[class*='TID_']").each(function() {
 // code is e here ...
});
Share Improve this question edited Jul 30, 2013 at 13:04 Kris Harper 5,8729 gold badges54 silver badges99 bronze badges asked Jul 30, 2013 at 9:01 Mihai ViteazuMihai Viteazu 1,6613 gold badges13 silver badges16 bronze badges 4
  • if jquery return an array then this will work $("div[class*='TID_']").reverse() – bugwheels94 Commented Jul 30, 2013 at 9:03
  • @Ankit: But, jQuery does not return an array. – Felix Kling Commented Jul 30, 2013 at 9:03
  • possible duplicate of JQuery .each() backwards – Felix Kling Commented Jul 30, 2013 at 9:04
  • Lets check ordering from this question: stackoverflow./questions/2351635/… – Maxim Zhukov Commented Jul 30, 2013 at 9:04
Add a ment  | 

4 Answers 4

Reset to default 12

Try

$("div[class*='TID_']").sort(function(e1, e2){
    return $(e1).attr('class') > $(e2).attr('class')
}).each(function() {
    console.log($(this).text())
});

Demo: Fiddle

You can use index to reverse the elements.

Live Demo

elements = $("div[class*='TID_']")
elements.each(function(index) {
  current = elements.eq(elements.length - index -1);
});
$($("div[class*='TID_']").get().reverse()).each(function() {
    console.log(this);
});

Working Example http://jsfiddle/yBZT6/

If your items are ordered in a descendant way all you need to do is a reverse each. You can do it - as proposed in this response - like this:

$($("div[class*='TID_']").get().reverse()).each(function() { /* ... */ });

本文标签: javascriptHow to start each from div id small to biggest numberStack Overflow