jQuery 排序 DIVS 第二部分
在 上一个问题中,我询问如何按 2 个参数对 div 进行排序:名称或值、升序或降序。 我用我得到的想法编写了所有代码,除了最后两个问题之外,我所有的问题都得到了解决。
这就是我所做的...为了对 div 进行排序,首先我连接值和 id 并拥有一个如下所示的数组:(
window.sortval = ["76#box1", "71#box122", "125#box4"];
我使用 window. 以确保其在全局范围内) 第一部分是我想要排序的数值,第二部分是 DIV 的 ID。
问题 1 是如果我运行
window.sortval.sort(function(a,b){return a - b})
它不会得到排序。
-- 为了继续我的实验,我使用浏览器控制台并手动对数组进行排序。现在我有第二个问题。我要用来进行实际排序的代码不起作用:
我有
<div id="container">
<div class="sortable" id="box1" rel="76" data-rel="Name One">
[some inside html including a table and divs]
</div>
<div class="sortable" id="box122" rel="71" data-rel="Name Two">
[some inside html including a table and divs]
</div>
<div class="sortable" id="box4" rel="125" data-rel="Name Three">
[some inside html including a table and divs]
</div>
</div>
,然后当我运行时
//get only the id part of each value in the array
var ids = $.map( window.sortname, function(val, i) {
return val.substring(val.indexOf('#'));
});
//sort the DOM
var cont = $('#container');
$.each(ids, function(i, v) {
cont.append($(v));
});
没有任何反应,甚至没有错误
In a previous question I asked how to sort divs by 2 parameters: name or value, asc or desc.
I wrote all the code with the idead I got and I got all solved but 2 last issues.
This is what I do... To sort the divs, first I concatenate the the value and id and have an array like this:
window.sortval = ["76#box1", "71#box122", "125#box4"];
(I use window. to make sure its in the global scope)
The first part is the numeric value I want to sort by and the second the ID of the DIV.
ISSUE 1 is that if I run
window.sortval.sort(function(a,b){return a - b})
it doesn't get sorted.
--
To go on with my experiment I used the browser console and sorted the array by hand. Now I have the second issue. The code I was going to use to do the actual sorting doesn't work:
I have
<div id="container">
<div class="sortable" id="box1" rel="76" data-rel="Name One">
[some inside html including a table and divs]
</div>
<div class="sortable" id="box122" rel="71" data-rel="Name Two">
[some inside html including a table and divs]
</div>
<div class="sortable" id="box4" rel="125" data-rel="Name Three">
[some inside html including a table and divs]
</div>
</div>
and then when I run
//get only the id part of each value in the array
var ids = $.map( window.sortname, function(val, i) {
return val.substring(val.indexOf('#'));
});
//sort the DOM
var cont = $('#container');
$.each(ids, function(i, v) {
cont.append($(v));
});
Nothing happens, not even an error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不需要该数组用于其他用途,这将对您的
.sortable
div 进行排序并将它们放入#container
中:http://jsfiddle.net/Gx5qw/
If you don't need the array for something else, this will sort your
.sortable
divs and put them inside#container
:http://jsfiddle.net/Gx5qw/
这就是您所需要的..
演示位于 http://jsfiddle.net/Wdybs/1/
This is what you need..
demo at http://jsfiddle.net/Wdybs/1/
这看起来是一种相当迂回的按两个键排序的方法。您应该能够使用带有自定义排序函数的标准
sort()
来实现这一点。这是一种方法,首先使用
rel
属性排序,然后使用id
:演示:http://jsfiddle.net/8F9Uv/
That looks like a rather roundabout way to sort by two keys. You should be able to that using a standard
sort()
with a custom sort function.Here's one way to do it, sorting first with the
rel
attribute followed by theid
:Demo: http://jsfiddle.net/8F9Uv/