使用 $.trim 的 $.trim 空间在 Jquery 中不起作用

$.trim space using $.trim doesn't work in Jquery

本文关键字:trim 不起作用 Jquery 空间 使用      更新时间:2023-09-26
        $(".p").each(function(i){
          len=$(this).text().length;
          if(len>80)
          {
            $(this).text($(this).text().substr(0,80)+'...');
          }
        });  

我的一些输出很好,比如

abc def...

但其中一些会像

1234 45 ...

如何修剪空间?我尝试了 $.trim 但不起作用。

这应该有效:

$(".p").text(function() {
    var text = $(this).text();
    if (text.length > 80) {
        return $.trim(text.substr(0, 80)) + '...';
    } else {
        return text;
    }
});