我在创建
后找到了这段代码<table> ... content ...
</ tables>
......我需要循环“FOR”的累计值“j”......
var dt = $(".table thead th").length;
for (var i = 0; i < dt; ++i) {
(function (j) {
var dt = $(".table thead th").length;
$(".table thead").each(function () {
var $ths = $(this).find('th');
if ($ths.length != 0 && $ths.eq(j).text() != "Estado" && $ths.eq(j).text() != "ESTADO" && $ths.eq(j).text() != "Acciones" && $ths.eq(j).text() != "ACCIONES") {
j = j + ","; // I need this value, the value to be displayed is something like this: 0,1,2,3 ...
alert(j);
}
});
return j;
})(i);
}
/* I need the value of "j" here, but this does not work */
var variable = j;
alert(variable);
答案 0 :(得分:3)
以下内容应该有效:
var dt = $(".table thead th").length;
var variable = "";
for (var i = 0; i < dt; ++i) {
variable += (function (j) {
var dt = $(".table thead th").length;
$(".table thead").each(function () {
var $ths = $(this).find('th');
if ($ths.length != 0 && $ths.eq(j).text() != "Estado" && $ths.eq(j).text() != "ESTADO" && $ths.eq(j).text() != "Acciones" && $ths.eq(j).text() != "ACCIONES") {
j = j + ",";
alert(j);
}
});
return j;
})(i);
}
alert(variable);
在您当前的代码j
由匿名函数返回,但它没有以任何方式使用,在for
循环中,您需要通过添加返回值来执行累积每次迭代的函数。
作为旁注,在循环外创建函数会更有效。
答案 1 :(得分:2)
我认为你在寻找的是
var indexes = $(".table thead th").map(function (i) {
var text = $.trim($(this).text());
if (text && text != "Estado" && text != "ESTADO" && text != "Acciones" && text != "ACCIONES") {
alert(i);
return i;
}
}).get().join(',')
甚至更短
var ignore = ["Estado", "ESTADO", "Acciones", "ACCIONES"]
var indexes = $(".table thead th").map(function (i) {
var text = $.trim($(this).text());
if (text && $.inArray(text, ignore) == -1) {
alert(i);
return i;
}
}).get().join(',')
答案 2 :(得分:1)
好的,快速而且肮脏,基于您现有的代码。只需要进行一项更改:
var variable;
var dt = $(".table thead th").length;
for (var i = 0; i < dt; ++i) {
(function (j) {
var dt = $(".table thead th").length;
$(".table thead").each(function () {
var $ths = $(this).find('th');
if ($ths.length != 0 && $ths.eq(j).text() != "Estado" && $ths.eq(j).text() != "ESTADO" && $ths.eq(j).text() != "Acciones" && $ths.eq(j).text() != "ACCIONES") {
j = j + ",";
alert(j);
}
});
variable += j;
return j;
})(i);
}
alert(variable);