我有以下脚本正常工作,但我需要添加一项检查,以便不会追加类的重复范围。
以下原始剧本
$.ajax({
type: "GET",
url: location.protocol + '//' + window.location.hostname + '/%YEAR%/export?TYPE=league&L=%LEAGUEID%&W=&JSON=1'
}).done(function (data) {
RequiredStarters = data.league.starters.count;
$.get('http://%HOST%/%YEAR%/options?L=%LEAGUEID%&O=06', function (data) {
$(data).find('td.two_column_layout .report').each(function (index, element) {
if ($(this).find("td.player").length !== +RequiredStarters && $(this).attr("id") !== "InvalidLineup") {
var target = $("#InvalidLineup").find("tbody");
$(this).find("span a").each(function () {
target.append("<tr class='oddtablerow'><td>" + $(this)[0].outerHTML + "</td><td class='lineupalert'><a class='lineuplink' href='http://%HOST%/%YEAR%/options?LEAGUE_ID=%LEAGUEID%&O=02&%FRANCHISEID%'>Submit Valid Lineup</a></td></tr>");
});
}
});
var $lineuprow = $("#InvalidLineup tr");
if ($lineuprow.length == 1) {
$lineuprow.text("All Teams Have Valid Lineups").addClass("oddtablerow").wrapInner("<td class='noevent'></td>");
}
});
});
});
我需要插入
var clazz = {};
$(this).find("span a").each(function () {
var className = this.className;
if (clazz[className]) {
return
}
clazz[className] = true;
target.append("<tr class='oddtablerow'><td>" + $(this)[0].outerHTML + "</td><td class='lineupalert'><a class='lineuplink' href='http://%HOST%/%YEAR%/options?LEAGUE_ID=%LEAGUEID%&O=02&%FRANCHISEID%'>Submit Valid Lineup</a></td></tr>");
})
替换此
$(this).find("span a").each(function () {
target.append("<tr class='oddtablerow'><td>" + $(this)[0].outerHTML + "</td><td class='lineupalert'><a class='lineuplink' href='http://%HOST%/%YEAR%/options?LEAGUE_ID=%LEAGUEID%&O=02&%FRANCHISEID%'>Submit Valid Lineup</a></td></tr>");
});
但是我必须做错事或者没有关闭某些东西,我不知道,使用jQuery新的并且我需要正确地格式化这个改变。
答案 0 :(得分:1)
这样的事情怎么样:
$(this).find("span a").each(function () {
// get class (assuming these items have only one class)
var className = $(this).attr('class');
// check for more than one
if ($('.' + className).length > 1) {
return;
} else {
target.append("the stuff you're appending");
}
});
或者作为与您开始时类似的替代方法:
var checkArray = [];
$(this).find("span a").each(function (ind, ele) {
var className = $(ele).attr('class');
if($.inArray(className, checkArray) < 0){
checkArray.push(className);
target.append("the stuff you're appending");
}
});