如何在文本中间显示文本溢出省略号

6

我正在使用带有固定宽度的Kendo网格。如果文本内容过多,将显示省略号。但是我需要根据字符串结尾来区分行,由于这种效果,我无法找到它。因此,我需要在文本中间添加省略号。

例如:

abcdefghijklm
abcdefg...  -> Normal Ellipse
abcd...klm  -> I Want need this type of output

我脑海中唯一的想法是减去最后三个字符,将前面的字符放在一个带有省略号的 span 中,然后紧接着加上最后三个字符。你觉得怎么样? - besciualex
你能接受在文本开头使用省略号吗?这在纯CSS中是无法实现的。 - Patrick Roberts
实际上,我不知道有什么纯CSS的方法可以做到这一点!我的解决方案涉及JavaScript和CSS两者。 - besciualex
你使用的是哪种包含这些字符串的HTML呢? - David Thomas
你可以在这里找到你想要的东西:https://dev59.com/l3RA5IYBdhLWcg3wyA_1 - Axel Cardinaels
@AxelCardinaels 谢谢。 - Anil Talla
2个回答

5
一种方法如下,当然这需要依赖于JavaScript:
function centralEllipsis(opts) {

    // the default settings, which can be overridden
    // by the user:
    var settings = {
        // the number of the original characters to show:
        'maxLength': 7,

        // the character-sequence, or HTML character-
        // entity to use to replace the missing characters:
        'ellipsis': '…',

        // the attribute to which you'd like to write the
        // original text (this function does also write
        // the text to the 'title' attribute though):
        'writeToAttribute': 'data-originaltext'
    },

   // the element upon which we're working (cached
   // because we'll access it more than once):
    _this = this;

    // iterating over the properties supplied by the user:
    for (var prop in opts) {

        // if the current property ('prop') of the object
        // ('opts') is enumerable and not from the prototype:
        if (opts.hasOwnProperty(prop)) {

            // we update that property in the settings object
            // (if the typeof the property-value ('opts[prop]')
            // is not equal to the string 'undefined', if it is
            // then we use the original property-value:
            settings[prop] = 'undefined' !== typeof opts[prop] ? opts[prop] : settings[prop];
        }
    }

    // we divide the settings.maxLength by 2 to work out
    // how many characters should appear at the beginning
    // of the string; using Math.ceil() to ensure whole
    // numbers:
    var prefixLength = Math.ceil(settings.maxLength / 2),

        // finding the length of the suffix by subtraction
        // of the prefixLength from the settings.maxLength:
        suffixLength = settings.maxLength - prefixLength,

        // setting the textContent of the current element
        // as the element's title text and storing it in
        // a variable:
        originalText = _this.title = _this.textContent,

        // empty variables initialised for later:
        prefix, suffix;

    // if the maxLength is less than the length of the original
    // text, then we go ahead (if not, we do nothing):
    if (settings.maxLength < originalText.length) {

        // storing the original text in the specified attribute:    
        _this.setAttribute(settings.writeToAttribute, originalText);

        // the prefix is the substring of the originalText, for
        // settings.maxLength number of characters starting at 
        // index 0 (the beginning of the string):
        prefix = originalText.substr(0, prefixLength);

        // if settings.maxLength is less than 2 then the
        // suffix is an empty string (''), otherwise it's
        // a substring of the originalText, using a negative
        // index which takes the last 'suffixLength'
        // characters from the string:
        suffix = settings.maxLength < 2 ? '' : originalText.substr(-suffixLength);

        // here we set the innerHTML (so that we can use HTML
        // character-entities, such as '&hellip;') to the
        // prefix + the settings.ellipsis character(s) + the suffix:
        _this.innerHTML = prefix + settings.ellipsis + suffix;
    }

}

// Using Function.prototype.call() to use Array.prototype.forEach()
// on the array-like NodeList returned by document.querySelectorAll():
Array.prototype.forEach.call(document.querySelectorAll('.midEllipsis'), function (el) {
// the first argument (here: 'el') supplied to the function
// is the array-element (here a DOM node from the NodeList)
// from the array (NodeList) over which we're iterating.

    // using Function.prototype.apply() in order to specify
    // that 'this' in the function (centralEllipsis) will be
    // the supplied DOM node ('el'); the array is used to 
    // pass the arguments to the function, the empty object
    // is what will be the 'opts' variable in the function
    // called. It doesn't have to be there, it's simply left
    // in place to show how to pass arguments to the function,
    // and how to supply user-defined options to override the
    // the defaults:
    centralEllipsis.apply(el, [{}]);
});

function centralEllipsis(opts) {
  var settings = {
      'maxLength': 7,
      'ellipsis': '&hellip;',
      'writeToAttribute': 'data-originaltext'
    },
    _this = this;

  for (var prop in opts) {
    if (opts.hasOwnProperty(prop)) {
      settings[prop] = 'undefined' !== typeof opts[prop] ? opts[prop] : settings[prop];
    }
  }

  var prefixLength = Math.ceil(settings.maxLength / 2),
    suffixLength = settings.maxLength - prefixLength,
    originalText = _this.title = _this.textContent,
    prefix, suffix;

  if (settings.maxLength < originalText.length) {

    _this.setAttribute(settings.writeToAttribute, originalText);

    prefix = originalText.substr(0, prefixLength);
    suffix = settings.maxLength < 2 ? '' : originalText.substr(-suffixLength);

    _this.innerHTML = prefix + settings.ellipsis + suffix;
  }

}

Array.prototype.forEach.call(document.querySelectorAll('.midEllipsis'), function(el) {
  centralEllipsis.apply(el, [{}]);
});
<ul>
  <li class="midEllipsis">abcdefghijklm</li>
</ul>

用于实验的外部JS Fiddle演示

要覆盖默认设置,例如将maxLength设置为4

Array.prototype.forEach.call(document.querySelectorAll('.midEllipsis'), function(el) {
  centralEllipsis.apply(el, [{
    'maxLength': 4
  }]);
});

function centralEllipsis(opts) {
  var settings = {
      'maxLength': 7,
      'ellipsis': '&hellip;',
      'writeToAttribute': 'data-originaltext'
    },
    _this = this;

  for (var prop in opts) {
    if (opts.hasOwnProperty(prop)) {
      settings[prop] = 'undefined' !== typeof opts[prop] ? opts[prop] : settings[prop];
    }
  }

  var prefixLength = Math.ceil(settings.maxLength / 2),
    suffixLength = settings.maxLength - prefixLength,
    originalText = _this.title = _this.textContent,
    prefix, suffix;

  if (settings.maxLength < originalText.length) {

    _this.setAttribute(settings.writeToAttribute, originalText);

    prefix = originalText.substr(0, prefixLength);
    suffix = settings.maxLength < 2 ? '' : originalText.substr(-suffixLength);

    _this.innerHTML = prefix + settings.ellipsis + suffix;
  }

}

Array.prototype.forEach.call(document.querySelectorAll('.midEllipsis'), function(el) {
  centralEllipsis.apply(el, [{
    'maxLength': 4
  }]);
});
<ul>
  <li class="midEllipsis">abcdefghijklm</li>
</ul>

外部JS Fiddle演示,用于实验。

或者将'ellipsis'字符设置为'»'字符(例如):

Array.prototype.forEach.call(document.querySelectorAll('.midEllipsis'), function(el) {
  centralEllipsis.apply(el, [{
    'ellipsis': '&raquo;'
  }]);
});

function centralEllipsis(opts) {
  var settings = {
      'maxLength': 7,
      'ellipsis': '&hellip;',
      'writeToAttribute': 'data-originaltext'
    },
    _this = this;

  for (var prop in opts) {
    if (opts.hasOwnProperty(prop)) {
      settings[prop] = 'undefined' !== typeof opts[prop] ? opts[prop] : settings[prop];
    }
  }

  var prefixLength = Math.ceil(settings.maxLength / 2),
    suffixLength = settings.maxLength - prefixLength,
    originalText = _this.title = _this.textContent,
    prefix, suffix;

  if (settings.maxLength < originalText.length) {

    _this.setAttribute(settings.writeToAttribute, originalText);

    prefix = originalText.substr(0, prefixLength);
    suffix = settings.maxLength < 2 ? '' : originalText.substr(-suffixLength);

    _this.innerHTML = prefix + settings.ellipsis + suffix;
  }

}

Array.prototype.forEach.call(document.querySelectorAll('.midEllipsis'), function(el) {
  centralEllipsis.apply(el, [{
    'ellipsis': '&raquo;'
  }]);
});
<ul>
  <li class="midEllipsis">abcdefghijklm</li>
</ul>

外部JS Fiddle演示,供实验使用。

参考文献:


超级...非常感谢。 - Anil Talla

0

你添加了CSS和JavaScript标签,因此我想到了一个解决方法,涉及到这两种语言。

你可以尝试下面的解决方法:
1. 从variable_a中减去除最后三个字符以外的所有字符。从variable_b中减去最后三个字符。
2. 将variable_a放入一个应用省略号的span中。
3. 紧接着第一个span,在另一个没有省略号的span中填充最后三个字符(variable_b)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接