Jquery.each数组推送不起作用

Jquery.each array push is not working?

本文关键字:不起作用 数组 each Jquery      更新时间:2023-09-26

我有这个代码:

var positions = [];
      $('.category-description TABLE TD').each(function() {
        var fulltxt = $(this).html().replace(/(<([^>]+)>)/ig,"");
        var lengt = fulltxt.length;
        var indx = $(this).index();
        positions.push[fulltxt];
        alert(positions);
      });

我不明白为什么它不起作用。。表总是至少有3个TD,fulltxt有内容。警报(仓位)返回空结果。

由于打字错误,它不起作用

positions.push[fulltxt];
              ^       ^

应该是

positions.push(fulltxt);
              ^       ^

看来你正试图重塑$(this).text()

您也可以使用map()

var positions = $('.category-description TABLE TD')
  .map(function() {
    return $(this).text();
  })
  .get();