我在页面上有一个块元素的集合。它们都有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的文本内容。


当前回答

添加到@Дмытрык的答案,缺失的边界和填充的演绎是完全有效的!!

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); let extra = 0; extra += parseFloat(styles.getPropertyValue('border-left-width')); extra += parseFloat(styles.getPropertyValue('border-right-width')); extra += parseFloat(styles.getPropertyValue('padding-left')); extra += parseFloat(styles.getPropertyValue('padding-right')); return text.width > (widthEl - extra); } .item{ width: 60px; overflow: hidden; text-overflow: ellipsis; } <div class="item">Short</div> <div class="item">Loooooooooooong</div>

其他回答

e.offsetWidth < e.scrollWidth解决方案并不总是有效。

如果你想使用纯JavaScript,我建议使用这个:

(打印稿)

public isEllipsisActive(element: HTMLElement): boolean {
    element.style.overflow = 'initial';
    const noEllipsisWidth = element.offsetWidth;
    element.style.overflow = 'hidden';
    const ellipsisWidth = element.offsetWidth;

    if (ellipsisWidth < noEllipsisWidth) {
      return true;
    } else {
      return false;
    }
}

如果你使用线钳>= 2线在多行添加省略号,你可以使用这个调节:

if (
      descriptionElement &&
      descriptionElement.offsetHeight < descriptionElement.scrollHeight
    ) {
      // has text-overflow
    }

所有的解决方案并没有真正为我工作,什么工作是比较元素scrollWidth其父(或子,取决于哪个元素有触发器)的scrollWidth。

当子进程的scrollWidth大于父进程的scrollWidth时,就意味着.text-省略号是活动的。


当el是父元素时

function isEllipsisActive(el) {
    let width       = el.offsetWidth;
    let widthChild  = el.firstChild.offsetWidth;
    return (widthChild >= width);
}

当el是子元素时

function isEllipsisActive(event) {
    let width       = el.offsetWidth;
    let widthParent = el.parentElement.scrollWidth;
    return (width >= widthParent);
}

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

$.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

在演示http://jsfiddle.net/brandonzylstra/hjk9mvcy/中https://stackoverflow.com/users/241142/iconoclast提到了一些错误。

在他的演示中,添加这些代码将工作:

setTimeout(() => {      
  console.log(EntryElm[0].offsetWidth)
}, 0)