我有一个跨度与动态数据在我的页面,与省略号风格。

.my-class
{
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;  
  width: 71px;
}
<span id="myId" class="my-class"></span>
document.getElementById('myId').innerText = "...";

我想在这个元素的工具提示中添加相同的内容,但我希望它只在内容较长且省略号出现在屏幕上时才会出现。 有什么办法可以做到吗? 当省略号被激活时,浏览器是否抛出一个事件?

*浏览器:Internet Explorer


当前回答

我们需要检测是否真的应用了省略号,然后显示一个工具提示来显示全文。仅仅比较“这个”是不够的。offsetWidth < this。scrollWidth"当元素几乎包含了它的内容,但宽度只多了一到两个像素,特别是对于全宽的中文/日语/韩语字符的文本。

这里有一个例子:http://jsfiddle.net/28r5D/5/

我发现了一种改进省略号检测的方法:

“这相比较。offsetWidth < this。首先,如果失败,继续步骤#2。 暂时切换css样式为{'overflow': 'visible', 'white-space': 'normal', 'word-break': 'break-all'}。 让浏览器做中继布局。如果发生换行,元素将扩展其高度,这也意味着需要省略号。 恢复css样式。

以下是我的改进:http://jsfiddle.net/28r5D/6/

其他回答

这是我的jQuery插件:

(function($) {
    'use strict';
    $.fn.tooltipOnOverflow = function() {
        $(this).on("mouseenter", function() {
            if (this.offsetWidth < this.scrollWidth) {
                $(this).attr('title', $(this).text());
            } else {
                $(this).removeAttr("title");
            }
        });
    };
})(jQuery);

用法:

$("td, th").tooltipOnOverflow();

编辑:

我已经为这个插件做了一个要点。 https://gist.github.com/UziTech/d45102cdffb1039d4415

这是一个香草JavaScript解决方案:

var cells = document.getElementsByClassName("cell"); for(const cell of cells) { cell.addEventListener('mouseenter', setTitleIfNecessary, false); } function setTitleIfNecessary() { if(this.offsetWidth < this.scrollWidth) { this.setAttribute('title', this.innerHTML); } } .cell { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border: 1px; border-style: solid; width: 120px; } <div class="cell">hello world!</div> <div class="cell">hello mars! kind regards, world</div>

我们需要检测是否真的应用了省略号,然后显示一个工具提示来显示全文。仅仅比较“这个”是不够的。offsetWidth < this。scrollWidth"当元素几乎包含了它的内容,但宽度只多了一到两个像素,特别是对于全宽的中文/日语/韩语字符的文本。

这里有一个例子:http://jsfiddle.net/28r5D/5/

我发现了一种改进省略号检测的方法:

“这相比较。offsetWidth < this。首先,如果失败,继续步骤#2。 暂时切换css样式为{'overflow': 'visible', 'white-space': 'normal', 'word-break': 'break-all'}。 让浏览器做中继布局。如果发生换行,元素将扩展其高度,这也意味着需要省略号。 恢复css样式。

以下是我的改进:http://jsfiddle.net/28r5D/6/

我有CSS类,它决定在哪里放省略号。在此基础上,我做以下操作(元素集可能不同,我写那些,使用省略号,当然它可以是一个单独的类选择器):

$(document).on('mouseover', 'input, td, th', function() {
    if ($(this).css('text-overflow') && typeof $(this).attr('title') === 'undefined') {
        $(this).attr('title', $(this).val());
    }
});

上面的解决方案对我都没用,但我想出了一个很棒的解决方案。人们犯的最大错误是在页面加载时在元素上声明所有3个CSS属性。你必须动态添加这些样式+工具提示当且仅当你想要一个椭圆的跨度比它的父宽。

    $('table').each(function(){
        var content = $(this).find('span').text();
        var span = $(this).find('span');
        var td = $(this).find('td');
        var styles = {
            'text-overflow':'ellipsis',
            'white-space':'nowrap',
            'overflow':'hidden',
            'display':'block',
            'width': 'auto'
        };
        if (span.width() > td.width()){
            span.css(styles)
                .tooltip({
                trigger: 'hover',
                html: true,
                title: content,
                placement: 'bottom'
            });
        }
    });