Using javascript substring() to create a read more link

后端 未结 3 1729
予麋鹿
予麋鹿 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:30

    var cutoff = 100;
    var text = $('div.contentdetail').text();
    var rest = text.substring(cutoff);
    if (text.length > cutoff) {
      var period = rest.indexOf('.');
      var space = rest.indexOf(' ');
      cutoff += Math.max(Math.min(period, space), 0);
    }
    // Assign the rest again, because we recalculated the cutoff
    rest = text.substring(cutoff);
    var visibleText = $('div.contentdetail').text().substring(0, cutoff);
    

    EDIT: shortened it a bit. EDIT: Fixed a bug EDIT: QoL improvement

提交回复
热议问题