admin管理员组文章数量:1300202
I'm not sure how to go about doing this. Let's say I have the following HTML code:
<div id="container">
<div class="item" order="5"></div>
<div class="item" order="3"></div>
<div class="item" order="4"></div>
<div class="item" order="1"></div>
<div class="item" order="2"></div>
</div>
Using jQuery, how could I order these divs based on the attribute "order"? By reorder, I mean I would like the dom to update the order of the divs based on the attribute "order". This would naturally adjust the z-index of the divs as well. I know that setting the z-index based on the attribute order will give me the correct display in the browser window, but it won't manipulate the dom order.
I assume I would loop through #container and get the child and its order like so:
$('#container > div').each( function(event) {
var itemOrder = $(this).attr('order');
});
...but I'm at a loss on how to manipulate the order. Any help would be greatly appreciated.
I'm not sure how to go about doing this. Let's say I have the following HTML code:
<div id="container">
<div class="item" order="5"></div>
<div class="item" order="3"></div>
<div class="item" order="4"></div>
<div class="item" order="1"></div>
<div class="item" order="2"></div>
</div>
Using jQuery, how could I order these divs based on the attribute "order"? By reorder, I mean I would like the dom to update the order of the divs based on the attribute "order". This would naturally adjust the z-index of the divs as well. I know that setting the z-index based on the attribute order will give me the correct display in the browser window, but it won't manipulate the dom order.
I assume I would loop through #container and get the child and its order like so:
$('#container > div').each( function(event) {
var itemOrder = $(this).attr('order');
});
...but I'm at a loss on how to manipulate the order. Any help would be greatly appreciated.
Share Improve this question edited Sep 14, 2013 at 15:32 Denys Séguret 382k90 gold badges810 silver badges775 bronze badges asked Sep 13, 2013 at 11:33 AndrewAndrew 2,8717 gold badges34 silver badges49 bronze badges 3- 4 what does this have to do with z-index ? – emre nevayeshirazi Commented Sep 13, 2013 at 11:34
- 3 Your unclear question has many different answers, please be more specific. – Ram Commented Sep 13, 2013 at 11:37
-
z-index
has nothing to do with DOM order. I'll edit the title to something relevant. – Reinstate Monica Cellio Commented Sep 13, 2013 at 11:40
5 Answers
Reset to default 10You can do this :
$('#container').append($('#container .item').sort(function(a,b){
return a.getAttribute('order')-b.getAttribute('order');
}));
Demonstration
$('#container > div').each( function(event) {
$(this).css('z-index', $(this).attr('order'));
});
I think u want this:DEMO
$('#container > div').sortElements(function(a, b){
return parseInt($(a).attr('order')) > parseInt($(b).attr('order')) ? 1 : -1;
});
Here is how to sort divs by attribute "tag" as a single word. JSFiddle has code to check for multiple words, etc. Posting because couldn't find solution when I was looking for it.
<div id="tag_sections">
<!-- populated with sorted divs -->
</div>
<div id="all_tags">
<div data-tag="blue">8</div>
<div>1</div>
<div data-tag="tag">2</div>
<div data-tag="red">3</div>
<div data-tag="blue">4</div>
<div>5</div>
<div data-tag="red">6</div>
<div data-tag="blue">7</div>
</div>
<script src="http://code.jquery./jquery-latest.min.js"></script>
<script type="text/javascript">
// sort all by title tag
var allTags = [];
$('#all_tags div').each(function() {
theTag = $(this).attr('data-tag');
if (!theTag) { theTag = 'ungrouped'; }
if (!$('#'+theTag).length) {
allTags.push(theTag);
$('#tag_sections').append('<div id="'+theTag+'"><h3>'+theTag+'</h3></div>');
}
if ($.inArray(theTag, allTags) > -1) {
$('#tag_sections #'+theTag).append($(this));
}
});
</script>
http://jsfiddle/zyEwF/269/
Demo of sorting by attribute value that is not a number and creates new div to add those sorted elements into.
Try to look here. Could be useful for understanding fundamental principles. http://james.padolsey./javascript/sorting-elements-with-jquery/
本文标签: javascriptReorder DIV39s based on element attributesStack Overflow
版权声明:本文标题:javascript - Reorder DIV's based on element attributes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741650048a2390419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论