简体   繁体   English

jQuery.each数组推送不起作用?

[英]Jquery.each array push is not working?

I have this code: 我有以下代码:

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);
      });

I can't understand why it's not working.. Table always have atleast 3 TD's and fulltxt have content. 我不明白为什么它不起作用。表始终包含至少3个TD,fulltxt具有内容。 Alert(positions) returns empty result. 警报(位置)返回空结果。

It does not work because of a typo 由于输入错误而无法使用

positions.push[fulltxt];
              ^       ^

should be 应该

positions.push(fulltxt);
              ^       ^

And it appears you are trying to reinvent $(this).text() . 看来您正在尝试重新发明$(this).text()

You can also use map() 您也可以使用map()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM