简体   繁体   English

jQuery函数用于多行省略号文本

[英]Jquery function for multiline ellipsis text

I've created a fiddle here: http://jsfiddle.net/mupersan82/JYuSY/ 我在这里创建了一个小提琴: http : //jsfiddle.net/mupersan82/JYuSY/

Multiline ellipsis function: 多行省略号功能:

$(function() {
var ellipsisText = $('.multilineEllipseText');
var textContainer = $('.incidentCellBottomRight').height();
while ($(ellipsisText).outerHeight(true) > textContainer) {
    $(ellipsisText).text(function(index, text) {
        return text.replace(/\W*\s(\S)*$/, '...');
    });
}

}); });

The function is taken from here: Cross browsers mult-lines text overflow with ellipsis appended within a width&height fixed div? 该函数取自此处: 跨浏览器多行文本溢出,并在宽度和高度固定div内附加省略号?

The function sort of works for multiline content but doesn't allow the content to extend to the edge. 该功能适用​​于多行内容,但不允许内容扩展到边缘。 With singleline content, the ellipsis is added after only one word. 对于单行内容,省略号仅在一个单词之后添加。

It should allow text up to the max height of its parent div, which is 100px. 它应该允许文本达到其父div的最大高度,即100px。

In your code you have: 在您的代码中,您具有:

var $ellipsisText = $('.multilineEllipseText');

this selects all .multilineEllipseText elements. 这将选择所有.multilineEllipseText元素。 Yet you are using .outerHeight(true) later on, which will return the height of the first element in the given set only . 然而,你正在使用.outerHeight(true)以后,将返回的第一个元素的高度,在给定的唯一设置。

So it works for the first element, but for each element after that, you're comparing to the wrong height (that from the first). 因此它适用于第一个元素,但之后的每个元素都与错误的高度(从第一个元素开始的高度)进行比较。


Here is how you can fix that: 解决方法如下:

var textContainerHeight= $('.incidentCellBottomRight').height();

$('.multilineEllipseText').each(function () {
  var $ellipsisText = $(this);

  while ($ellipsisText.outerHeight(true) > textContainerHeight) {
    $ellipsisText.text(function(index, text) {
      return text.replace(/\W*\s(\S)*$/, '...');
    });
  }
});

demo: http://jsfiddle.net/JYuSY/1/ 演示: http : //jsfiddle.net/JYuSY/1/

Hope this will help you. 希望这会帮助你。 ( dotdotdot ) dotdotdot

http://dotdotdot.frebsite.nl/ http://dotdotdot.frebsite.nl/

I know this answer is very old, but I thought I'd share a similar problem and solution since it's very relevant. 我知道这个答案很老,但是我认为我也有类似的问题和解决方案,因为它非常相关。

I have a responsive site that has some paragraphs that need to cut off after 3 lines and add a link that essentially exposes the remaining text when clicked, for aesthetic reasons. 我有一个响应式网站,该网站的某些段落在3行之后需要被截断,并出于美学原因添加了一个链接,该链接在单击时实质上暴露了其余文本。

So it's crucial that I keep all of the text handy rather than refresh the page every time the screen size changes (that's the purpose of responsive). 因此,至关重要的是,我必须保持所有文本的方便,而不是每次屏幕大小更改时都刷新页面(这是响应的目的)。

Anyway, here's my elegant solution: 无论如何,这是我的优雅解决方案:

/** 
  * @name   - Ellipsify
  * @desc   - trims and adds ellipsis to element when element text height is greater than element height
  * @param  - [required] (string)identity - string containing jquery selector used to create object group (array) based on selector
  * @function : _init - calculates each element and determines need for ellipsis, then truncates text as needed
*/
var Ellipsify = function(identity) {
    if (identity) {
        this.identity = identity;
        this.group = $(this.identity);
        if (this.group.length > 0) {
            this._init();
        }
    }
};
Ellipsify.prototype = {
    _init : function() {
        var that = this;
        that.group.each(function() {
            if ($(this).children('.hidden').length > 0) {
                $(this).children(':not(.hidden)').text($(this).children(':not(.hidden)').text().replace('...',' ') + $(this).children('.hidden').text());
                $(this).children('.hidden').remove();
            }
            if ($(this).children().outerHeight() > $(this).innerHeight()) {
                $(this).append('<span class="hidden"></span>');
                while ($(this).children(':not(.hidden)').outerHeight() > $(this).innerHeight()) {
                    var paragraph = $(this).children(':not(.hidden)').text().replace('...', '');
                    var lastword = paragraph.substring(paragraph.lastIndexOf(' '));
                    $(this).children(':not(.hidden)').text(paragraph.substring(0, paragraph.lastIndexOf(' ')) + '...');
                    $(this).children('.hidden').text(lastword + $(this).children('.hidden').text());
                }
            }
        });
    },
};

The HTML looks like this: HTML看起来像这样:

<p><span>Big paragraph of text goes here.</span></p>

and the CSS is simply: CSS就是:

p {
    font-size:1em;
    line-height:1.5em;
    height:4.5em;
    width:100%;
    overflow:hidden;
}
.hidden {
    display:none;
}

Full-word multi-line word-wrap with ellipsis: http://jsfiddle.net/tdpLdqpo/4/ 带省略号的全字多行自动换行: http : //jsfiddle.net/tdpLdqpo/4/

Check out the JSFiddle. 查看JSFiddle。 Just set these two properties: 只需设置以下两个属性:

var maxWidth = 300;
var maxLines = 3;
$(".jsgrid-cell").each(function(i,v){
    var txt=$(v).text();
    if(txt.length>100){
        var shortText=txt.substring(0, 100)+
        "<span onclick='$(this).hide();$(this).next().toggle();'>"+
            "..."+
        "</span>"+
        "<span  style='display:none'>"+
            txt.substring(100, txt.length)+
        "</span>";

        $(v).html(shortText );
    }
});

Jquery function for single line ellipsis text. jQuery函数用于单行省略号文本。 You can use this function for character count as well in textbox, span or any other DOM element. 您也可以在文本框,跨度或任何其他DOM元素中将此功能用于字符计数。 You just have to pass Text (String) (DOM element Value) and Size (Int) (In number) 您只需要传递文本(字符串)(DOM元素值)和大小(整数)(数量)

// Character Count Ellipsis
function EllipsisChar(text, size) {
    var len = text.length;
    if (len >= size) {
        // if text length is equal to or more than given size then add ...
        text = text.substring(0, size) + "...";
    }
    else {
        // if text length is less then do something
    }
    return text;
};

Here is the example how to use: 这是示例如何使用:

var EllipsisReturnText = EllipsisChar("Sample text goes here", 10);

Result: 结果:

"Sample tex..."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM