【发布时间】:2015-02-02 23:54:35
【问题描述】:
尝试根据数据属性对子 div 进行排序
下面的 html 代码是由 CM 生成的,数据可以按任意随机顺序检索。
html代码是
<section class="box explore">
<div id="ProductContainer" class="row">
<div id="1232132" data-name="B" data-category="Category_A" class="explore-cell">
<h>B</h>
<p>Category_A</p>
</div>
<div id="123" data-name="A" data-category="Category_A" class="explore-cell">
<h>A</h>
<p>Category_A</p>
</div>
<div id="1232152351" data-name="C" data-category="Category_A" class="explore-cell">
<h>C</h>
<p>Category_A</p>
</div>
<div id="12342341" data-name="E" data-category="Category_B" class="explore-cell">
<h>E</h>
<p>Category_B</p>
</div>
<div id="1325321" data-name="D" data-category="Category_B" class="explore-cell">
<h>D</h>
<p>Category_B</p>
</div>
</div>
java
$('div').sort(function (a, b) {
var contentA = $(a).attr('data-name');
var contentB = $(b).attr('data-name');
return (contentA < contentB) ? -1 : (contentA > contentB) ? 1 : 0;
})
Jsfiddle http://jsfiddle.net/w8gkshue/
如果有人能正确地告诉我如何按产品名称或类别进行最佳排序。
更新希望这能提供更好的解释
【问题讨论】:
-
您是否尝试根据您的排序重新定位 dom 元素?
-
div 会生成行和单元格的网格视图,其中单元格是无序的。我试图使用stackoverflow.com/questions/6133723/… 作为起点,但没有使用字符串
标签: javascript jquery html