javascript – 使用jQuery对元素进行排序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用jQuery对元素进行排序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2373字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 使用jQuery对元素进行排序](/upload/InfoBanner/zyjiaocheng/709/355f57839a5a4314a48054faac2b46a9.jpg)
我的网站上有以下HTML代码:
<div class="groups">
<div class="group">
Group 1 priority:
<select>
<option value="1.0">1</option>
<option value="2.0" selected="selected">2</option>
<option value="3.0">3</option>
</select>
</div>
<div class="group">
Group 2 priority:
<select>
<option value="1.0">1</option>
<option value="2.0">2</option>
<option value="3.0" selected="selected">3</option>
</select>
</div>
<div class="group">
Group 3 priority:
<select>
<option value="1.0" selected="selected">1</option>
<option value="2.0">2</option>
<option value="3.0">3</option>
</select>
</div>
</div>
我正在寻找一种方法来根据下拉列表中选择的内容,使用jQuery对这些组在浏览器中显示的顺序进行排序.当用户在任何下拉列表中或页面加载中选择新值时,它应该求助.
这个问题最简单的方法是什么?
我可以使用jQuery UI,如果可以以任何方式使用可排序的东西.我无法找到使用它的方法.
更新:< div class =“group”>中还有其他数据.无论他们搬到哪里都应该遵循下拉菜单.组的数量从0到20不等.
解决方法:
编辑:这里有一些代码可以做你想要的.这里select_1,select_2等应该是下拉列表的ID. getDropdown()应该是一个函数,它使用您选择的方法(document.getElementById().options.selectedIndex ,, jquery等)返回给定下拉ID的选定值.
<div id="parent">
<div id="child1">
..content
<select id="select_1">...content</select>
</div>
<div id="child2">
..content
<select id="select_2">...content</select>
</div>
<div id="child3">
..content
<select id="select_3">...content</select>
</div>
</div>
function sortValues()
{
/*Save the contents of each div in an array
so they can be looped through and
re inserted later*/
var content=[$("#child1").html(),$("#child2").html,$("#child3").html()];
//Get the value of all dropdowns and sort them
var sortedArray=[getDropdown("select_3"),getDropdown("select_2"),getDropdown("select_3")];
var sortedContent=new Array();
sortedArray.sort();
/*Loop through all the sorted values,
compare the value of each dropdown
against the sorted value and use that
to determine the new arrangement of the divs
*/
for (x=0; x< sortedArray.length; x++)
{
for (y=0; y<=content.length; y++)
{
if (getDropdown("dropdown_" + (y+1))==sortedArray[x])
{
sortedContent[x]=content[x];
//This will prevent the same div from being assigned again:
$("#select_" + (y+1)).remove();
break;
}
}
}
/* Empty the parent div so new divs can be inserted.
You can also do a fadeout/fadeIn of the div here
*/
$("#parent").empty();
for (x=0; x< sortedContent.length; x++)
{
$("#parent").append(sortedContent[x]);
}
}
内容总结
以上是互联网集市为您收集整理的javascript – 使用jQuery对元素进行排序全部内容,希望文章能够帮你解决javascript – 使用jQuery对元素进行排序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。