10

我正在尝试检测(通过 javascript)文本溢出何时生效。经过大量研究,我有一个可行的解决方案,除了任何版本的 Firefox:

http://jsfiddle.net/tonydew/mjnvk/

如果您调整浏览器以应用省略号,Chrome、Safari 甚至 IE8+ 都会提醒省略号处于活动状态。在 Firefox(我尝试过的每个版本,包括 17 和 18)中没有那么多。Firefox 将始终告诉您省略号未激活。

console.log() 输出显示了原因:

Firefox (OS X):
116/115 - false
347/346 - false

Chrome (OS X):
116/115 - false
347/851 - true 

在 Firefox 中,scrollWidth 永远不会大于 offsetWidth。

我能找到的最接近解决方案的是“为什么 IE 和 Firefox 会为 div 返回不同的溢出尺寸? ”但我已经在使用建议的解决方案。

任何人都可以阐明如何在 Firefox 中进行这项工作吗?


编辑:除了下面的@Cezary 答案外,我还发现了一种不需要更改标记的方法。然而,它做了更多的工作,因为它临时克隆了每个元素来进行测量:

$(function() {
    $('.overflow').each(function(i, el) {
        var element = $(this)
                      .clone()
                      .css({display: 'inline', width: 'auto', visibility: 'hidden'})
                      .appendTo('body');

        if( element.width() > $(this).width() ) {
            $(this).tooltip({
                title: $(this).text(),
                delay: { show: 250, hide: 100 },
            });
        }
        element.remove();
    });
});

http://jsfiddle.net/tonydew/gCnXh/

有人对此的效率发表评论吗?如果我有许多潜在溢出元素的页面,这会产生负面影响吗?如果可以的话,我想避免修改现有的标记,但不会以每次页面加载时过多的 JS 处理为代价。

4

2 回答 2

6

您需要在每个 td 中添加 div 以使其在 firefox 中工作,

<td class="first"><div>Here is some text</div></td>
<td class="second">
     <div>Here is some more text. A lot more text than
     the first one. In fact there is so much text you'd think it was a 
     waste of time to type all ofit.
     </div>
</td>

CSS

td div {
   white-space: nowrap;
   text-overflow: ellipsis;
   overflow:hidden;
   width:100%;
}

提琴手

http://jsfiddle.net/mjnvk/7/

于 2013-02-01T09:22:03.680 回答
3

我实际上写了一个 jQuery 插件来做到这一点。如果截断,它只是将title目标元素的 设置为整个文本,但您可以根据您的确切需求对其进行调整:

/**
 * @author ach
 *
 * Sets the CSS white-space, overflow, and text-overflow properties such that text in the selected block element will
 * be truncated and appended with an ellipsis (...) if overflowing.  If the text is truncated in such a way, the
 * selected element's 'title' will be set to its full text contents and the cursor will be set to 'default'.
 * For this plugin to work properly, it should be used on block elements (p, div, etc.).  If used on a th or td element,
 * the plugin will wrap the contents in a div -- in this case, the table's 'table-layout' CSS property should be set to 'fixed'.
 *
 * The default CSS property values set by this plugin are:
 *     white-space: nowrap;
 *     overflow: hidden;
 *     text-overflow: ellipsis
 *
 * @param cssMap A map of css properties that will be applied to the selected element.  The default white-space,
 * overflow, and text-overflow values set by this plugin can be overridden in this map.
 *
 * @return The selected elements, for chaining
 */

$.fn.truncateText = function(cssMap) {
    var css = $.extend({}, $.fn.truncateText.defaults, cssMap);

    return this.each(function() {
        var $this = $(this);
        //To detect overflow across all browsers, create an auto-width invisible element and compare its width to the actual element's
        var element = $this.clone().css({display: 'inline', width: 'auto', visibility: 'hidden'}).appendTo('body');
        if (element.width() > $this.width()) {
            //If a th or td was selected, wrap the content in a div and operate on that
            if ($this.is("th, td")) {
                $this = $this.wrapInner('<div></div>').find(":first");
            }
            $this.css(css);
            $this.attr("title", $.trim($this.text()));
            $this.css({"cursor": "default"});
        }
        element.remove();
    });
};
$.fn.truncateText.defaults = {
    "white-space"   : "nowrap",
    "overflow"      : "hidden",
    "text-overflow" : "ellipsis"
};

要使用,只需包含 js 并调用:

$(".override").truncateText();

这已在生产中使用,并且没有注意到页面上有数百个目标元素有任何不良影响。

于 2013-02-01T20:42:57.233 回答