从可排序列表中获取所有元素属性

时间:2022-06-30 19:00:22

So i have a List of divs which are sortable using the Jquery UI option sortable.
Those divs contain some custom attributes using the setAttribute method like:
.setAttribute("Nr", "Nr_1") and .setAttribute("V", "V_1"). I already can get an array of an singel Attribute using the .toArray method

所以我有一个div列表,可以使用可排序的Jquery UI选项进行排序。这些div包含一些使用setAttribute方法的自定义属性,如:.setAttribute(“Nr”,“Nr_1”)和.setAttribute(“V”,“V_1”)。我已经可以使用.toArray方法获取一个singel属性数组

$("#NameContainer").sortable("toArray", { attribute: 'value'});

$("#NameContainer").sortable("toArray", { attribute: 'Nr' });

$(“#NameContainer”)。sortable(“toArray”,{attribute:'value'}); $(“#NameContainer”)。sortable(“toArray”,{attribute:'Nr'});

$("#NameContainer").sortable("toArray", { attribute: 'V' });


 <div id="NameContainer" class="ui-widget">
      <div value="Name_1" id="Name_1">John</div>
      <div value="Name_2" id="Name_2">Jack</div>
      <div value="Name_3" id="Name_3">Charlie</div>
      <div value="Name_4" id="Name_4">Sawyer</div>
      <div value="Name_5" id="Name_5">Yin</div>
      <div value="Name_6" id="Name_6">Ben</div>
    </div>

My Question now is how can i get all Attributes from every Elemente in the right order after they got sorted or course in a singel variable.

UPDATE:
So my first solution that is working as intented looks like that:

我的问题现在是如何在每个Elemente按照正确的顺序获取所有属性后,在它们被排序或在一个singel变量中。更新:所以我的第一个解决方案就像那样工作:

for(var i = 0; i<ArrayList.length; i++){
var attributeArray = [arrayValue[i], arrayNr[i], arrayV[i] ];
mainArray.push(attributeArray);
}

And then call the main: mainArray[ ] [1]

然后调用main:mainArray [] [1]

1 个解决方案

#1


0  

EDIT: Was only doing it for the main div container..fixed:

编辑:只为主div容器做..修复:

If you want to get the attributes in the order they are found then I think you can do something along the lines of:

如果你想按照它们的顺序获取属性,那么我认为你可以做以下事情:

var el = $('#NameContainer > div');

var nodes=[], values=[];
el.each(function() {
    for (var att, i = 0, atts = $(this).attributes, n = atts.length; i < n; i++){
        att = atts[i];
        nodes.push(att.nodeName);
        values.push(att.nodeValue);
    }
});

that will give you two arrays where the index i of nodes will give you the attribute name and the same index i of values will be the accompanying value of that attribute. this can be restructured in a number of different ways but this is a simple way to achieve your goal.

这将为您提供两个数组,其中节点的索引i将为您提供属性名称,值的相同索引i将是该属性的附带值。这可以通过多种不同方式进行重组,但这是实现目标的简单方法。

#1


0  

EDIT: Was only doing it for the main div container..fixed:

编辑:只为主div容器做..修复:

If you want to get the attributes in the order they are found then I think you can do something along the lines of:

如果你想按照它们的顺序获取属性,那么我认为你可以做以下事情:

var el = $('#NameContainer > div');

var nodes=[], values=[];
el.each(function() {
    for (var att, i = 0, atts = $(this).attributes, n = atts.length; i < n; i++){
        att = atts[i];
        nodes.push(att.nodeName);
        values.push(att.nodeValue);
    }
});

that will give you two arrays where the index i of nodes will give you the attribute name and the same index i of values will be the accompanying value of that attribute. this can be restructured in a number of different ways but this is a simple way to achieve your goal.

这将为您提供两个数组,其中节点的索引i将为您提供属性名称,值的相同索引i将是该属性的附带值。这可以通过多种不同方式进行重组,但这是实现目标的简单方法。