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

标签 javascript jquery

        $(".p").each(function(i){
          len=$(this).text().length;
          if(len>80)
          {
            $(this).text($(this).text().substr(0,80)+'...');
          }
        });  

我的一些输出很好,比如

abc def...

但其中一些会像

1234 45 ...

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

最佳答案

这应该有效:

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

关于javascript - 使用 $.trim 的 $.trim 空间在 Jquery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27727620/

相关文章:

jQuery 的 getScript - 将文件包含到主范围中?

javascript - 在 Div 中显示可折叠的内容

javascript - WordPress 中链接 php 和 javascript 错误

javascript - 请解释为什么它 "doesn' t 甚至改变数字的值是有意义的”

javascript - 移除 JavaScript 中不需要的逗号

javascript - jquery fn.extend() 返回值和调用对象中的值

javascript - 有没有办法在普通 Javascript 中存储先前单击的元素

jquery - css3 动画与 scaleX 和 scaleY?

jquery - 如何让 jQuery 与 Prototype 一起工作

c# - 应用程序启动时的 Asp.net MVC 进度条