简体   繁体   English

截断字符串

[英]Truncate string

I have a script: 我有一个脚本:

  _content_insert: function () {
        var e = this,
            t = this.$tooltip.find(".tooltipster-content");        
        if (typeof e.Content === "string" && !e.options.contentAsHTML) {
            t.text(e.Content)
        } else {
            t.empty().append(e.Content)
        }
    },

and I'd like to truncate the Content to 140 characters. 并且我想将内容截断为140个字符。

I found a truncating script here - Truncate a string straight javascript 我在这里找到了截断脚本- 截断字符串

var length = 3;
var myString = "ABCDEFG";
var myTruncatedString = myString.substring(0,length);

How do I implement this into the code so that what comes out in (e.Content) is the truncated 140-character string? 如何在代码中实现这一点,以便(e.Content)中出现的是截断的140个字符的字符串?

t.text(e.Content.substring(0,140));

Doesn't it work? 不行吗

_content_insert: function () {
    var e = this,
        t = this.$tooltip.find(".tooltipster-content");        
    if (typeof e.Content === "string" && !e.options.contentAsHTML) {
        t.text(e.Content.substring(0,140))
    } else {
        t.empty().append(e.Content.substring(0,140))
    }
},

You can optimize the code. 您可以优化代码。 You can truncate after the string check and use that var. 您可以在字符串检查后截断并使用该变量。

Try this... 尝试这个...

_content_insert: function () {
    var e = this,
        t = this.$tooltip.find(".tooltipster-content");        
    if (typeof e.Content === "string" && !e.options.contentAsHTML) {
        if(e.Content.length>140){
           t.text(e.Content.substring(0,140))
        }
        else{
           t.text(e.Content);
        }
    } else {
        t.empty().append(e.Content)
    }
},

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

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