Show only first two rows from a paragraph

后端 未结 4 815
我在风中等你
我在风中等你 2021-01-15 15:09

I need to display some text on two lines, and add some \"...\" if the text is too long.

Example :

This is some long text and we need to show it on only two         


        
4条回答
  •  野的像风
    2021-01-15 16:06

    var string = "This is some long text and we need to show it on only two lines.".split(" ");
    var first_line = string.slice(0,5).join(" ");
    var second_line = string.slice(6,10).join(" ");
    
    var display_string = first_line + "
    " + second_line; if(string.length > 10) display_string += "..."; $("#text").html(display_string);

    There is definitely a much better way to do this, but this will at least get you started. It shows 5 words per line and adds and ellipsis if there are more than 10 words. Seek a better solution but you can use this until you find one. http://jsfiddle.net/hepW8/

提交回复
热议问题