Display only 10 characters of a long string?

前端 未结 12 2086
臣服心动
臣服心动 2020-12-13 01:19

How do I get a long text string (like a querystring) to display a maximum of 10 characters, using JQuery?

Sorry guys I\'m a novice at JavaScript & JQuery :S

12条回答
  •  独厮守ぢ
    2020-12-13 02:05

    html

    Some very very very very very very very very very very very long string

    javascript (on doc ready)

    var longText = $('#longText');
    longText.text(longText.text().substr(0, 10));
    

    If you have multiple words in the text, and want each to be limited to at most 10 chars, you could do:

    var longText = $('#longText');
    var text = longText.text();
    var regex = /\w{11}\w*/, match;
    while(match = regex.exec(text)) {
        text = text.replace(match[0], match[0].substr(0, 10));
    }
    longText.text(text);
    

提交回复
热议问题