Javascript 如何使用jQuery循环处理HTML元素和JSON?

Javascript 如何使用jQuery循环处理HTML元素和JSON?,javascript,jquery,json,Javascript,Jquery,Json,我试图循环使用类名steem_verify遍历所有元素,然后在根据我的API验证的用户名之后附加“verifyed”。我终于让它工作了(某种程度上),但在验证了名称之后,它会显示“已验证”,所以我猜我的循环在某个地方出错了 这是我的密码: $.getJSON("https://steemverify.com/api/verified.json", function (data) { $.each(data, function (i, item) { $(".steem_v

我试图循环使用类名steem_verify遍历所有元素,然后在根据我的API验证的用户名之后附加“verifyed”。我终于让它工作了(某种程度上),但在验证了名称之后,它会显示“已验证”,所以我猜我的循环在某个地方出错了

这是我的密码:

$.getJSON("https://steemverify.com/api/verified.json", function (data) {
    $.each(data, function (i, item) {
        $(".steem_verify").each(function (index) {
            var username = $(this).text();
            $(data).each(function () {
                if (item.username == username) {
                    $(".steem_verify")[index].append(" Verified");
                }
            });
        });
    });
});

根本不需要第一个循环

$.getJSON(“https://steemverify.com/api/verified.json,函数(数据){
//$。每个(数据、功能(i、项){
//警报(item.username);
$(“.steem_verify”)。每个(函数(索引){
var username=$(this.text();
$(数据)。每个(功能(i,项){
如果(item.username==用户名){
$(“.steem_verify”)[index].append(“verifyed”);
}
});
});
//});
});

磨蹭
steemitqa

steemverify
无需第一个循环

$.getJSON(“https://steemverify.com/api/verified.json,函数(数据){
//$。每个(数据、功能(i、项){
//警报(item.username);
$(“.steem_verify”)。每个(函数(索引){
var username=$(this.text();
$(数据)。每个(功能(i,项){
如果(item.username==用户名){
$(“.steem_verify”)[index].append(“verifyed”);
}
});
});
//});
});

磨蹭
steemitqa
steemverify
通过OP验证解决方案

此代码适用于:

$.getJSON("https://steemverify.com/api/verified.json", function (data) {
    $.each(data, function (i, item) {
        $(".steem_verify").each(function (index) {
            var username = $(this).text();
            if (item.username === username) {
                $(".steem_verify")[index].append(" Verified");
            }
        });
    });
});
解决方案由OP

此代码适用于:

$.getJSON("https://steemverify.com/api/verified.json", function (data) {
    $.each(data, function (i, item) {
        $(".steem_verify").each(function (index) {
            var username = $(this).text();
            if (item.username === username) {
                $(".steem_verify")[index].append(" Verified");
            }
        });
    });
});

$。每个(data
$(data)。每个
内部看起来都有17种错误。我不熟悉jQuery中的循环。第一个允许我使用诸如item.verify之类的json变量,第二个允许我使用类“steem\u verify”循环所有元素,第三个用于检查用户名是否存在。jQuery中的循环与任何其他循环一样……是否有帮助我将您的解决方案移动到社区wiki答案中。
$。每个(数据)
$(数据).每个
内部看起来都有17种错误。我对jQuery中的循环不太熟悉。第一种允许我使用诸如item.verify之类的json变量,第二种允许我使用类“steem_verify”遍历所有元素,第三个让我检查用户名是否存在。jQuery中的循环与任何其他循环一样……这有帮助吗?我已将您的解决方案移至社区wiki答案。