简体   繁体   English

这段代码有什么作用? (带数组值的jQuery排序元素)

[英]What does this code do? (jQuery sort element with array value)

I was looking for an answer to one of my questions and found this question right here: 我正在寻找我的一个问题的答案,并在这里找到了这个问题:
jquery sort element with array value 具有数组值的jquery排序元素

And there is a brilliant answer from fantactuka solving my problem. fantessuka解决我的问题有一个很好的答案。 But I don't quite understand how this code works: Full Code 但我不太明白这段代码的工作原理: 完整代码

​var order = [3, 2, 4, 1]​;
var el = $('#sort');
var map = {};

$('#sort div').each(function() { 
    var el = $(this);
    map[el.attr('n')] = el;
});

for (var i = 0, l = order.length; i < l; i ++) {
    if (map[order[i]]) {
        el.append(map[order[i]]);
    }
} 

Could someone explain what this part of his code does? 有人可以解释他的代码的这部分是做什么的吗?

JavaScript Loops JavaScript循环

Loops are handy, if you want to run the same code over and over again, each time with a different value. 循环是很方便的,如果你想一遍又一遍地运行相同的代码,每次都有不同的值。

Often this is the case when working with arrays: Instead of writing: 使用数组时通常就是这种情况:而不是写:

text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";

You can write: 你可以写:

for (i = 0; i < cars.length; i++) {
    text += cars[i] + "<br>";
}

http://www.w3schools.com/js/js_loop_for.asp http://www.w3schools.com/js/js_loop_for.asp


ARRAY MAP 阵列地图

Return an array with the square root of all the values in the original array: 返回一个数组,其中包含原始数组中所有值的平方根:

var numbers = [4, 9, 16, 25];

function myFunction() {
    x = document.getElementById("demo")
    x.innerHTML = numbers.map(Math.sqrt);
}

The result will be: 2,3,4,5 结果将是: 2,3,4,5

http://www.w3schools.com/jsref/jsref_map.asp http://www.w3schools.com/jsref/jsref_map.asp


YOUR EXAMPLE 你的例子

​var order = [3, 2, 4, 1]​; //Map Order
var el = $('#sort'); //Get ID of target control
var map = {}; //Create empty map

$('#sort div').each(function() {  //For each control of ID ...
    var el = $(this); //Set el as input id ...
    map[el.attr('n')] = el; //add an attribute called n to each matching control
});

for (var i = 0, l = order.length; i < l; i ++) { //loop through array up to the length of array
    if (map[order[i]]) { //If map equals current iteration
        el.append(map[order[i]]); //Apply order
    }
} 

FLEXBOX Flexbox的

On a side note the effect you are trying to achieve can be quite easy to achieve if you are willing to use the css3 flex-box which has sorting functionality. 另外,如果您愿意使用具有排序功能的css3 flex-box ,那么您尝试实现的效果可以很容易实现。

http://www.w3schools.com/cssref/css3_pr_order.asp http://www.w3schools.com/cssref/css3_pr_order.asp

So in first loop (each loop on div), he stores actual DOM elements in object named map . 因此,在第一个循环(div中的每个循环)中,他将实际DOM元素存储在名为map对象中。 That means after completion of this loop we have a object like this: 这意味着在完成此循环后,我们有一个这样的对象:

map = {
    "1": div with attr 1,
    "2": div with attr 2,
    "3": div with attr 3,
    .
    .
}

Now we have one array in which we have store order of elements. 现在我们有一个数组,其中我们有元素的商店顺序。

So in looping on that array [3, 2, 1, 4]: 所以在循环上该数组[3,2,1,4]:

when i = 0;
order[i] = 3
map[order[i]] = map[3] = div with attr 3 
appending this div on element

when i = 1;
order[i] = 2
map[order[i]] = map[2] = div with attr 2 
appending this div on element

when i = 2;
order[i] = 1
map[order[i]] = map[1] = div with attr 1 
appending this div on element

and so on 等等

This piece of code takes a list of DOM elements and reorders the elements in the specified fashion. 这段代码获取DOM元素列表并以指定方式重新排序元素。

Here is a working fiddle 这是一个工作小提琴

var order = [3, 2, 4, 1]​;
var el = $('#sort');
var map = {};

In the above code, order holds the required arrangement, el references the container element. 在上面的代码中, order保持所需的排列, el引用容器元素。

$('#sort div').each(function() { 
    var el = $(this);
    map[el.attr('n')] = el;
});

Above, a map is created with current index vs DOM element. 在上面,使用当前索引与DOM元素创建map It looks like this: 它看起来像这样:

map = {
    1: <element with n=1>, 
    2: <element with n=2>, 
    3: <element with n=3>, 
    4: <element with n=4>
}

Finally, the below code reorders the DOM by comparing map and order 最后,下面的代码通过比较maporder重新排序DOM

for (var i = 0, l = order.length; i < l; i ++) {
    if (map[order[i]]) {
        el.append(map[order[i]]);
    }
} 

The above loop runs this way: 上面的循环以这种方式运行:

el.append(map[3])
el.append(map[2])
el.append(map[4])
el.append(map[1])

Since you are appending an element already in el into el , el.append removes it and adds it to the end. 由于您将el的元素追加到el ,因此el.append将其删除并将其添加到结尾。 That's how the reordering happens. 这就是重新排序的方式。 Check this fiddle to understand that 检查这个小提琴 ,了解这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM