按名为“数据字母”的每个属性按字母顺序排序
sort alphatically by each attribute named "data-letter"
我想根据选择框中所选选项的值按字母顺序对具有"数据字母"属性的div进行排序。例如,如果用户在选择框中选择"A",那么每个具有"数据字母"属性的div将按字母顺序排列,例如"A,B,C,D,E等"基于相应的"数据字母"属性内容。任何想法,帮助,线索,建议和建议将不胜感激。谢谢!
网页结构
<div class="select_holder">
<select>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div class="box_container">
<div data-letter="B">
this is the content of box
</div>
<div data-letter="A">
this is the content of box
</div>
<div data-letter="C">
this is the content of box
</div>
<div data-letter="C">
this is the content of box
</div>
<div data-letter="A">
this is the content of box
</div>
<div data-letter="A">
this is the content of box
</div>
<div data-letter="C">
this is the content of box
</div>
</div>
我的脚本
$(document).ready(function(){
$("select").change(function(){
//$(this).val();
//I dont know how to make it like sort it up alphatically :(
});
});
$(document).ready(function(){
$("select").change(function(){
var value = this.options[this.selectedIndex].value;
var box = $('.box_container');
box.find('div').sort(function(a,b){
if(a.getAttribute('data-letter') == value){
return 0;
}
if( b.getAttribute('data-letter') == value){
return 1;
}
return (a.getAttribute('data-letter') > b.getAttribute('data-letter')?1:-1);
}).appendTo(box);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select_holder">
<select>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
<div class="box_container">
<div data-letter="B">
this is the content of box B
</div>
<div data-letter="A">
this is the content of box A
</div>
<div data-letter="C">
this is the content of box C
</div>
<div data-letter="C">
this is the content of box C
</div>
<div data-letter="A">
this is the content of box A
</div>
<div data-letter="Z">
this is the content of box Z
</div>
<div data-letter="A">
this is the content of box A
</div>
<div data-letter="C">
this is the content of box C
</div>
<div data-letter="B">
this is the content of box B
</div>
</div>
相关文章:
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- 如何使用下划线根据自定义排序顺序对对象数组进行排序
- 如何根据不同排序顺序的多个值对json对象进行排序
- 如何使用Jquery Data表更正排序顺序
- 使用Async.js sortBy的升序与降序排序顺序
- 如何在Jquery中使用给定的任何特定排序顺序对字符串数组进行排序
- JavaScript 排序顺序错误
- 如何在 select2() 字段中保存排序顺序
- Jquery DataTable 组排序顺序
- 按可排序顺序获取所有文本区域内容
- 是否可以按当前排序顺序获取跨多个页面的所有 jqGrid 行
- 使用数组作为排序顺序
- 如何将其转换为排序顺序
- 打开 Graph API 排序顺序,或简单的 jQuery/JavaScript 排序
- 谷歌地图 API - 最近的点 - 更改排序顺序
- 如何保存排序顺序
- 反转角度中的排序顺序
- 迭代 JavaScript 集合并添加排序顺序
- 根据数字排序顺序更新自动重新排列HTML列表
- 在backbone.js中,如何使用新的模型数据更新视图,同时在没有完整渲染的情况下保持排序顺序