admin管理员组文章数量:1241124
I have a requirement where in i have to display the text for two lines & add ... if its overflowing My div Width is fixed.(Even height can be considered as fixed).
Actual text looks like this:
1
Lorem Ipsum is simply
dummy text of the printing
and typesetting industry.
2
Lorem Ipsum is simply text
of the printing and types
industry.
Expected:
1
Lorem Ipsum is simply
dummy text of the print...
2
Lorem Ipsum is simply text
of the printing and typ...
I do not want to overload with plugins(three dots jquery plugin does that).
I'm planning to do just by cutting(split) the string as div width is fixed.
Thanks in Advance.
I have a requirement where in i have to display the text for two lines & add ... if its overflowing My div Width is fixed.(Even height can be considered as fixed).
Actual text looks like this:
1
Lorem Ipsum is simply
dummy text of the printing
and typesetting industry.
2
Lorem Ipsum is simply text
of the printing and types
industry.
Expected:
1
Lorem Ipsum is simply
dummy text of the print...
2
Lorem Ipsum is simply text
of the printing and typ...
I do not want to overload with plugins(three dots jquery plugin does that).
I'm planning to do just by cutting(split) the string as div width is fixed.
Thanks in Advance.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 29, 2010 at 0:49 jaanjaan 1431 gold badge2 silver badges9 bronze badges6 Answers
Reset to default 6UPDATE: Looks like text-overflow
is only useful for horizontal truncation.
Here's a little jQuery that should work.
Try it out: http://jsfiddle/UfxYQ/
var $container = $('#container');
var $text = $('#text');
var originalText = $text.text();
var temp = originalText;
if( $container.outerHeight() < $text.outerHeight() ) {
while($container.outerHeight() < $text.outerHeight()) {
$text.text( temp = temp.substr(0, temp.length-1) );
}
$text.text( temp = temp.substr(0, temp.length-3) );
$text.append('...');
}
HTML
<div id='container'>
<span id='text'>Lorem Ipsum is simply
dummy text of the printing
and typesetting industry.
</span>
</div>
CSS
#container {
width: 150px;
height: 50px;
line-height: 25px;
position: relative;
}
You won't have full cross browser support, but close. There's a CSS property called text-overflow
that is supported in IE6+, Safari, Konqueror, and Opera (with an Opera specific property).
#myOverflowDiv {
text-overflow: ellipsis;
-o-text-overflow: ellipsis;
}
http://www.quirksmode/css/textoverflow.html
http://www.quirksmode/css/contents.html#t413
The CSS text-overflow property is the nicest way, but it's not supported by all browsers yet. If you want to use Javascript, you could create an offscreen element. Use CSS (generated by the script) to give it the same width and font as your target, with a height of '2em' (i.e. 2 lines). Run a loop, adding the text into it one letter at a time, until either you run out of letters or the height changes. When the height increases you know you've exceeded two lines.
Of course you need to account for the width of the "..." as well, so you might append it to the letter you're about to add.
Be sure the element is offscreen rather than using "display: none", as non-displayed elements have no height. "visibility: hidden" might work; I haven't tried.
Just an addition for post of user113716. Cleaner approach of doing this:
var $container = $('#container');
var $text = $('#text');
var temp = $text.text();
if ($container.height() < $text.outerHeight()) {
while($container.height() < $text.outerHeight()) {
temp = temp.substr(0, temp.length-1)
$text.text(temp + '...');
}
}
http://jsfiddle/YR7F2/
As the overflow ellipsis css doesn't work for multiline text you have to do it with JavaScript. You could copy the text box, position it absolute with top -1000000px and cut the last letter of the text until the box has the right height. Or you simple cut after a specific letter count this will be faster but not accurate.
A neat way to do it would be with a little jQuery plugin...
$.fn.trimToParentHeight = function(options) {
options = $.extend($.fn.trimToParentHeight.defaults, options);
return this.each(function(index, text) {
text = $(text);
var height = parseInt(text.parent().height());
var temp = text.text();
while(parseInt(text.outerHeight()) > height && temp.length>0) {
temp = temp.substr(0, temp.length-1)
text.text(temp + options.ellipsis);
}
});
}
$.fn.trimToParentHeight.defaults = {
ellipsis: '...'
};
Then you can just do...
$(".container .text").trimToParentHeight()
... as long as the container is the direct parent of the text.
string="test/firstpage/secondpage/thirdpage";
string.split("/").slice(0, 1);
Out put will be : test
本文标签:
版权声明:本文标题:jquery - How to cut the string in javascript so that it fits exactly two lines & add ... at the end - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740065760a2222813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论