如何在每个匹配的链接旁边添加文字?
for (var i = 0; i < author.length; i++) {
var adminID = author[i]["href"].match(/\/users\/(\d+)/g);
for (var j = 0; j < userID.length; j++) {
if (adminID == userID[j]) {
$(`a[href*="${userID[j]}"]`).append('<span>text</span>');
}
}
}
答案 0 :(得分:0)
添加一个标志以表明它已被添加
for (var i = 0; i < author.length; i++) {
var adminID = author[i]["href"].match(/\/users\/(\d+)/g);
var appended = false;
for (var j = 0; j < userID.length; j++) {
if (adminID == userID[j] && !appended) {
appended = true;
$(`a[href*="${userID[j]}"]`).append('<span>text</span>');
}
}
}
答案 1 :(得分:0)
之后break
:
for (var i = 0; i < author.length; i++) {
var adminID = author[i]["href"].match(/\/users\/(\d+)/g);
for (var j = 0; j < userID.length; j++) {
if (adminID == userID[j]) {
$(`a[href*="${userID[j]}"]`).append('<span>text</span>');
break;
}
}
}