开发者

.each() > $(this).find().eq().html() is null

Why don't work?

var str;

$('table tr').each(function() {
    str = $(this).find('td').eq(6).html().trim().substring(10开发者_开发技巧, 20);
    $(this).find('td').eq(6).text(str);
});

Need write in all 6th trim+substring+another_string_action with same value.

Firebug write error: $(this).find("td").eq(6).html() is null


Two things.

First, remember that not all browsers have a native .trim() function. Safer to use jQuery's $.trim().

  • http://api.jquery.com/jquery.trim/

Second, remember that .eq() takes a 0 based index, so if you want the sixth <td> column, pass 5. Right now you're asking for the seventh <td>.

var str;

$('table tr').each(function() { 
    var $td = $(this).find('td').eq(5); // Get sixth <td> in the row
    str = $.trim( $td.html() ).substring(10, 20);
    $td.text(str);
});


You should avoid repeating the lookup of the table cell anyway:

$('table tr').each(function() {
  var cell = $(this).find('td').eq(6);
  if (cell.length) {
    cell.text(cell.html().trim().substring(10, 20));
  }
});

It might also be good to check the original string to make sure it's as long as the code expects. Finally, you get the contents with .html() but then you set with .text(). Is that really what you want?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜