Jquery each()类,只在循环中获取元素

时间:2021-11-12 14:28:38

I am trying to use an each() function and every time the class appears in the table it gets the information within that section.

我正在尝试使用each()函数,每当类出现在表中,它就会获得该段中的信息。

It is doing the loop x3 but the information i am asking for is only getting from the first instance.

它做的是循环x3,但我所要求的信息只是从第一个实例得到的。

<td class="approved_player"> 
<span first_role="1" role="Damage" class="icon_damage"></span>   
<a class="class_monk">Sickli</a>
</td>

After it has read the information it updates the json array 1 in either damage or not.

在读取信息之后,它会以损坏或不损坏的方式更新json数组1。

var classes = {
    classes: [{
        "mage": 0
    }, {
        "warlock": 0
    }, {
        "rogue": 0
    }, {
        "paladin": 0
    }, {
        "deathknight": 0,
            "deathknight damage": 0
    }, {
        "monk": 0,
            "monk damage": 0
    }, {
        "priest": 0,
            "priest damage": 0
    }, {
        "druid": 0,
            "druid damage": 0
    }, {
        "shaman": 0,
            "shaman damage": 0
    }]
};

$("td.approved_player").each(function (i, obj) {
    var player = $("a[class^='accountLink class_']").attr('class').split('_')[1];
    var role = $("span[first_role='1']").attr('role').toLowerCase();

    $.each(classes, function (key, data) {
        $.each(data, function (index, data) {
            if (player == data) {
                if (role == 'damage') {
                    data[1] = 1;
                } else {
                    data[0] = 1;
                }
            }
        });
    });

    $("div#output").append(i + ' appoved class ' + player + ' role ' + role + '<br/>');

});


    $.each(classes, function (key, data) {
        $.each(data, function (index, data) {
            console.log('index', data);
        });
    });

I am working on this here: http://jsfiddle.net/EXwsy/1/

我正在这里研究这个:http://jsfiddle.net/EXwsy/1/

Thanks for any help.

感谢任何帮助。

1 个解决方案

#1


1  

Well i found an error

我发现了一个错误

Here you're comparing you data object with the string of the player

在这里,您将数据对象与播放器的字符串进行比较

 if (player == data) 

I changed this to:

我改变了这个:

if (Object.keys(data)[0] == player)

In this way you will compare the "monk" with the first property of your object in this case is the mage one.

这样你就可以比较“修道士”和你的对象的第一个属性在这种情况下就是法师属性。

And also this will retrieve 3 elements

这也会得到3个元素

  $("td.approved_player")

But inside your each loop you get an element like this but it will retrieve 3 elements

但是在每个循环中你会得到一个这样的元素但是它会检索3个元素

$("a[class^='accountLink class_']")

This will also retrieve 3 elements and you work always on the first.

这也将检索3个元素,您总是在第一个元素上工作。

Using this you will retrieve the current element.

使用它,您将检索当前元素。

$(this).children("a").attr('class').split('_')[1]

Here is the working fiddle well i think so.

这是工作小提琴,我想是的。

Just remember that jQuery can retrieve multiple DOM objects from a desired selector.

只需记住,jQuery可以从所需的选择器中检索多个DOM对象。

#1


1  

Well i found an error

我发现了一个错误

Here you're comparing you data object with the string of the player

在这里,您将数据对象与播放器的字符串进行比较

 if (player == data) 

I changed this to:

我改变了这个:

if (Object.keys(data)[0] == player)

In this way you will compare the "monk" with the first property of your object in this case is the mage one.

这样你就可以比较“修道士”和你的对象的第一个属性在这种情况下就是法师属性。

And also this will retrieve 3 elements

这也会得到3个元素

  $("td.approved_player")

But inside your each loop you get an element like this but it will retrieve 3 elements

但是在每个循环中你会得到一个这样的元素但是它会检索3个元素

$("a[class^='accountLink class_']")

This will also retrieve 3 elements and you work always on the first.

这也将检索3个元素,您总是在第一个元素上工作。

Using this you will retrieve the current element.

使用它,您将检索当前元素。

$(this).children("a").attr('class').split('_')[1]

Here is the working fiddle well i think so.

这是工作小提琴,我想是的。

Just remember that jQuery can retrieve multiple DOM objects from a desired selector.

只需记住,jQuery可以从所需的选择器中检索多个DOM对象。