我正在尝试使用类名steem_verify遍历所有元素,然后在根据我的API验证的用户名后添加“已验证”。我终于让它工作了(有点),但在经过验证的名字之后它说“Verified Verified Verified”,所以我猜我的循环在某处乱了。
这是我的代码:
$.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");
}
});
});
});
});
答案 0 :(得分:1)
根本不需要第一次循环
$.getJSON("https://steemverify.com/api/verified.json", function (data) {
//$.each(data, function (i, item) {
//alert(item.username);
$(".steem_verify").each(function (index) {
var username = $(this).text();
$(data).each(function ( i , item) {
if (item.username == username) {
$(".steem_verify")[index].append(" Verified");
}
});
});
//});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="steem_verify">dalt</div>
<div class="steem_verify">steemitqa</div>
<div class="steem_verify">steemverify</div>
以另一种方式,您只需要一个循环并使用.filter()
,因此您的代码看起来很简单
$.getJSON("https://steemverify.com/api/verified.json", function (data) {
$.each(data, function (i, item) {
$(".steem_verify").filter(function(){return item.username == $(this).text() }).append(" Verified");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="steem_verify">dalt</div>
<div class="steem_verify">steemitqa</div>
<div class="steem_verify">steemverify</div>
答案 1 :(得分:1)
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");
}
});
});
});