jQuery插件& quot; readmore&" ;,在不剪断单词的情况下修剪文本 [英] jQuery Plugin "readmore", trim text without cutting words
问题描述
我正在使用 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.
这篇关于jQuery插件& quot; readmore&" ;,在不剪断单词的情况下修剪文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!