通过jQuery的文本字数限制
HTML: 这很有效,但有时评论会这样:通过jQuery的文本字数限制,jquery,html,css,Jquery,Html,Css,HTML: 这很有效,但有时评论会这样: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 但我想: <p class="comment">Lorem ipsum dolor si...</p> 好的,我需要jQuery函数来实现-而不是字符-字限制 我怎样才能解决这个问题?多谢各位 <p class="comment">Lorem ipsum dolor sit...</
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
但我想:
<p class="comment">Lorem ipsum dolor si...</p>
好的,我需要jQuery函数来实现-而不是字符-字限制
我怎样才能解决这个问题?多谢各位
<p class="comment">Lorem ipsum dolor sit...</p>
您还可以使用插件:
jQuery扩展插件
作为字符串的扩展
用作
String.prototype.trimToLength = function(m) {
return (this.length > m)
? jQuery.trim(this).substring(0, m).split(" ").slice(0, -1).join(" ") + "..."
: this;
};
更多源代码示例,请始终尝试用谷歌搜索
编辑2:对于您的示例,请检查使用此代码并享受………: .评论{ 宽度:10em; 溢出:隐藏; 文本溢出:省略号; 空白:nowrap; }
Lorem ipsum door sit amet ipsum door sit amet…
不确定您是否正在寻找类似的产品,但我会尝试一下"This is your title".trimToLength(10);
{
试试这个
如果超出了行数限制,我们将以三个字符为单位逐步减少文本,因为我们不仅要删除文本的最后一个字符,还要删除换行符的两个控制字符。您已经选择了一个答案,但对于其他正在寻找同一问题解决方案的人,我建议您尝试以下方法: 您可以在此处看到此代码: JS代码:代码中的注释
if(txtArea.value.length > maxChars || txtArea.scrollHeight > maxHeight)
宽度为180px的初始css参数给出:
Lorem ipsum dolor sit amet ipsum dolor sit amet
使用set_ellipses_by_word可得出:
我添加了一个答案,看一看,告诉我你的想法。@Karmaron他发布的内容有很多选项:内联、jquery、字符串原型等。选择你的毒药。如果不是动态内容,请在服务器上修剪它!@BobBrown客户端修剪是示例2。
function splitOnLastWord(text, charLimit){
var splitText = text.split(" "),
wordCount = 0,
finalWord = "";
$.each(splitText, function(i, v){
wordCount += v.length;
finalWord += v + ' ';
if( wordCount > charLimit ){
finalWord += '...';
return false;
}
});
return finalWord;
}
splitOnLastWord("Lorem ipsum dolor sit amet ipsum dolor sit amet...", 19);
if(txtArea.value.length > maxChars || txtArea.scrollHeight > maxHeight)
<script>
function set_ellipses_by_word(txt_element, max_width){
// if max_width is undefined, we take current text element width
max_width = typeof max_width == 'undefined' ? $(txt_element).width() : max_width;
// get the text element type
var elm_type = $(txt_element)[0].nodeName;
// init vars
var txt = $(txt_element).text(),
// convert our text to an array
arr = txt.split(' '),
str = '', current_width = 0,
// you can adjust this value according to your font and font-size ...
max_margin = 4;
// create a temporary element for the test, it should have the same font properties of the original element
$('body').append('<'+elm_type+' class="txt_temp" style="display:block;float:left;"></'+elm_type+'>');
for(var i=0; i<arr.length; i++){
// we use str to add words everytime
// i = 0 : str = "Lorem"
// i = 1 : str = "Lorem ipsum"
// i = 3 : str = "Lorem ipsum dolor sit"
// ...
str += (str != '' ? ' ' : '' ) + arr[i];
// set our temporary text element text
$('.txt_temp').text(str + '...');
// compare our temporary text element width and our max width, It should lower than our max width
if($('.txt_temp').width() < max_width){
current_width = $('.txt_temp').width();
}
}
// remove temporary text element
$('.txt_temp').remove();
if(current_width > 0){
// set ou text element width
$(txt_element).css('width', current_width + max_margin + 'px');
}
}
// original text is :
// Lorem ipsum dolor sit amet ipsum dolor sit amet
// text visible with initial css params with 180px width gives :
// on chrome 38 : Lorem ipsum dolor sit a...
// on firefox 33 : Lorem ipsum dolor sit ame...
// on internet explorer 10 : Lorem ipsum dolor sit a...
// using set_ellipses_by_word gives :
// on chrome 38 : Lorem ipsum dolor sit...
// on firefox 33 : Lorem ipsum dolor sit amet...
// on internet explorer 10 : Lorem ipsum dolor sit...
set_ellipses_by_word($('.comment'));
</script>
Lorem ipsum dolor sit amet ipsum dolor sit amet
on chrome 38 : Lorem ipsum dolor sit a...
on firefox 33 : Lorem ipsum dolor sit ame...
on internet explorer 10 : Lorem ipsum dolor sit a...
on chrome 38 : Lorem ipsum dolor sit...
on firefox 33 : Lorem ipsum dolor sit amet...
on internet explorer 10 : Lorem ipsum dolor sit...