Using javascript substring() to create a read more link

后端 未结 3 1735
予麋鹿
予麋鹿 2020-12-11 18:33

I\'m developing a Classic ASP page that pulls some content from a database and creates a Read more link after the first 100 characters as follows;

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 19:22

    Here is a fairly simple approach to getting endings at the word level, and shooting for about your given limit in characters.

    var limit        = 100,
        text         = $('div.contentdetail').text().split(/\s+/),
        word,
        letter_count = 0,
        trunc        = '',
        i            = 0;
    
    while (i < text.length && letter_count < limit) {
      word         = text[i++];
      trunc       += word+' ';
      letter_count = trunc.length-1;
    
    }
    
    trunc = $.trim(trunc)+'...';
    console.log(trunc);
    

提交回复
热议问题