我在页面上有一个块元素的集合。它们都有CSS规则white-space, overflow, text-overflow设置,以便溢出的文本被修剪并使用省略号。

但是,并非所有元素都溢出。

有没有办法,我可以使用javascript来检测哪些元素溢出?

谢谢。

添加:示例HTML结构我正在工作。

<td><span>Normal text</span></td>
<td><span>Long text that will be trimmed text</span></td>

SPAN元素总是适合单元格,它们应用了省略号规则。我想检测何时将省略号应用于SPAN的文本内容。


当前回答

@ItaloBorssatto的解决方案是完美的。但在看SO -之前,我做了一个决定。这就是:)

const elems = document.querySelectorAll('span'); elems.forEach(elem => { checkEllipsis(elem); }); function checkEllipsis(elem){ const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const styles = getComputedStyle(elem); ctx.font = `${styles.fontWeight} ${styles.fontSize} ${styles.fontFamily}`; const widthTxt = ctx.measureText(elem.innerText).width; if (widthTxt > parseFloat(styles.width)){ elem.style.color = 'red' } } span.cat { display: block; border: 1px solid black; white-space: nowrap; width: 100px; overflow: hidden; text-overflow: ellipsis; } <span class="cat">Small Cat</span> <span class="cat">Looooooooooooooong Cat</span>

其他回答

我的实现)

const items = Array.from(document.querySelectorAll('.item')); items.forEach(item =>{ item.style.color = checkEllipsis(item) ? 'red': 'black' }) function checkEllipsis(el){ const styles = getComputedStyle(el); const widthEl = parseFloat(styles.width); const ctx = document.createElement('canvas').getContext('2d'); ctx.font = `${styles.fontSize} ${styles.fontFamily}`; const text = ctx.measureText(el.innerText); return text.width > widthEl; } .item{ width: 60px; overflow: hidden; text-overflow: ellipsis; } <div class="item">Short</div> <div class="item">Loooooooooooong</div>

这些解决方案都不适合我,所以我选择了一种完全不同的方法。我没有使用带有省略号的CSS解决方案,而是从特定的字符串长度中剪切文本。

  if (!this.isFullTextShown && this.text.length > 350) {
    return this.text.substring(0, 350) + '...'
  }
  return this.text

并显示“更多/更少”按钮,如果长度超过。

  <span
    v-if="text.length > 350"
    @click="isFullTextShown = !isFullTextShown"
  >
    {{ isFullTextShown ? 'show less' : 'show more' }}
  </span>

来自italo的回答非常好!不过,让我稍微细化一下:

function isEllipsisActive(e) {
   var tolerance = 2; // In px. Depends on the font you are using
   return e.offsetWidth + tolerance < e.scrollWidth;
}

跨浏览器兼容性

实际上,如果您尝试上面的代码并使用console.log打印出e.offsetWidth和e.scrollWidth的值,您将注意到,在IE上,即使您没有进行文本截断,也会出现1px或2px的值差异。

所以,根据你使用的字体大小,允许一定的容忍度!

此示例显示单元格表上的工具提示,文本被截断。是基于表宽度动态的:

$.expr[':'].truncated = function (obj) {
    var element = $(obj);

    return (element[0].scrollHeight > (element.innerHeight() + 1)) || (element[0].scrollWidth > (element.innerWidth() + 1));
};

$(document).ready(function () {
    $("td").mouseenter(function () {
        var cella = $(this);
        var isTruncated = cella.filter(":truncated").length > 0;
        if (isTruncated) 
            cella.attr("title", cella.text());
        else 
            cella.attr("title", null);
    });
});

演示:https://jsfiddle.net/t4qs3tqs/

它适用于所有版本的jQuery

除了italo的答案,还可以使用jQuery来实现。

function isEllipsisActive($jQueryObject) {
    return ($jQueryObject.width() < $jQueryObject[0].scrollWidth);
}

另外,正如Smoky指出的那样,您可能希望使用jQuery outerWidth()而不是width()。

function isEllipsisActive($jQueryObject) {
    return ($jQueryObject.outerWidth() < $jQueryObject[0].scrollWidth);
}