jQuery插件“readmore”,修剪文本而不切割单词 [英] jQuery Plugin "readmore", trim text without cutting words

查看:123
本文介绍了jQuery插件“readmore”,修剪文本而不切割单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 http://rockycode.com/blog/jquery-plugin-readmore / 用于修剪长文本并添加查看更多链接以显示所有文本。

I'm using http://rockycode.com/blog/jquery-plugin-readmore/ for trim long text and add a "See more" link to reveal all the text.

我希望避免剪切字词,我该怎么办?那个?

I would love to avoid cutting words, how could I do that?

如果限制为35,请不要削减w ...

If the limit is 35, don't cut the w...

但是

如果限制是35,请不要删除单词...(在这种情况下,将其修剪为38,然后显示隐藏的文本,从第39个结尾处开始。

If the limit is 35, don't cut the word... (and in this case, trim it at 38 and then show the hidden text from 39th chtill the end.

推荐答案

您可以更改该插件中的 abridge 功能,如下所示:

You can change the abridge function within that plugin as follows:

function abridge(elem) {
  var opts = elem.data("opts");
  var txt = elem.html();
  var len = opts.substr_len;
  var dots = "<span>" + opts.ellipses + "</span>";
  var charAtLen = txt.substr(len, 1);
  while (len < txt.length && !/\s/.test(charAtLen)) {
      len++;
      charAtLen = txt.substr(len, 1);
  }
  var shown = txt.substring(0, len) + dots;
  var hidden = '<span class="hidden" style="display:none;">' + txt.substring(len, txt.length) + '</span>';
  elem.html(shown + hidden);
}

...它会按照您的意愿行事。你可能想要添加一个选项来关闭和打开此功能,但我会把它留给你。

...and it will behave as you desire. You might want to add an option to turn this feature off and on, but I'll leave that up to you.

查看工作示例&#x2192;

这篇关于jQuery插件“readmore”,修剪文本而不切割单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆