Text-overflow: Firefox 4中的省略号? (和 FF5)

text-overflow:ellipsis; CSS 属性必须是为数不多的事情之一,微软已经为网络做了正确的。

所有其他浏览器现在都支持它... ... 除了 Firefox。

Firefox 开发人员已经使用了 从2005年就开始争论了,但是尽管有明显的需求,他们似乎不能真正实现它(即使是一个实验性的 -moz-实现也足够了)。

几年前,有人找到了通往 黑客火狐3,使其支持省略号的路。黑客使用 -moz-binding特性使用 XUL 实现它。现在有相当多的网站正在使用这种黑客技术。

坏消息是,Firefox 4是 删除 -moz-binding特性,这意味着这种黑客攻击不会再起作用了。

所以一旦 Firefox 4发布(我听说这个月晚些时候会发布) ,我们又要面对它不能支持这个特性的问题了。

所以我的问题是,还有别的办法吗?(如果可能的话,我尽量避免回到 Javascript 解决方案)

[编辑]
很多赞成票,所以我显然不是唯一一个想知道的人,但是到目前为止我得到了一个答案,基本上就是“使用 javascript”。我仍然希望有一个解决方案,或者根本不需要 JS,或者最坏的情况下只是把它用作 CSS 功能不起作用的备用方案。所以我要悬赏这个问题,万一有人在某个地方找到了答案。

[编辑]
更新: Firefox 已经进入快速开发模式,但是尽管 FF5现在已经发布,这个特性仍然不受支持。现在,大多数用户已经从 FF3.6升级,黑客攻击不再是一个解决方案。好消息是,我被告知 也许吧将被添加到 Firefox 6中,新的发布时间表将在几个月内发布。如果是这样的话,我想我可以等等,但很遗憾他们没能早点解决。

[最终编辑]
我看到省略号功能最终被添加到 Firefox 的“ Aurora Channel”(即开发版本)中。这意味着它现在应该作为火狐7的一部分发布,火狐7将于2011年底发布。真是松了一口气。

发行说明: https://developer.mozilla.org/en-US/Firefox/Releases/7

21692 次浏览

Spudley, you could achieve the same thing by writing a small JavaScript using jQuery:

var limit = 50;
var ellipsis = "...";
if( $('#limitedWidthTextBox').val().length > limit) {
// -4 to include the ellipsis size and also since it is an index
var trimmedText = $('#limitedWidthTextBox').val().substring(0, limit - 4);
trimmedText += ellipsis;
$('#limitedWidthTextBox').val(trimmedText);
}

I understand that there should be some way that all browsers support this natively (without JavaScript) but, that's what we have at this point.

EDIT Also, you could make it more neat by attaching a css class to all those fixed width field say fixWidth and then do something like the following:

$(document).ready(function() {
$('.fixWidth').each(function() {
var limit = 50;
var ellipsis = "...";
var text = $(this).val();
if (text.length > limit) {
// -4 to include the ellipsis size and also since it is an index
var trimmedText = text.substring(0, limit - 4);
trimmedText += ellipsis;
$(this).val(trimmedText);
}
});
});//EOF

This pure CSS solution is really close, except for the fact that it causes ellipsis to appear after every line.

I must say I'm a little disappointed that the only browser specific hack in my application is going to be to support FF4. The above javascript solution doesn't account for variable width fonts. Here is a more verbose script that accounts for this. The big problem with this solution is that if the element containing the text is hidden when the code is run then the width of the box isn't known. This was a deal breaker for me so I stopped working on/testing it... but I thought I'd post it here in case it is of use to someone. Be sure to test it well as my testing was less than exhaustive. I intended to add a browser check to only run the code for FF4 and let all the other browsers use their existing solution.

This should be available for fiddling here: http://jsfiddle.net/kn9Qg/130/

HTML:

<div id="test">hello World</div>

CSS:

#test {
margin-top: 20px;
width: 68px;
overflow: hidden;
white-space: nowrap;
border: 1px solid green;
}

Javascript (uses jQuery)

function ellipsify($c){
// <div $c>                 content container (passed)
//    <div $b>              bounds
//       <div $o>           outer
//          <span $i>       inner
//       </div>
//       <span $d></span>   dots
//    </div>
// </div>


var $i = $('<span>' + $c.html() + '</span>');
var $d = $('<span>...</span>');
var $o = $('<div></div>');
var $b = $('<div></div>');


$b.css( {
'white-space' : "nowrap",
'display' : "block",
'overflow': "hidden"
}).attr('title', $c.html());


$o.css({
'overflow' : "hidden",
'width' : "100%",
'float' : "left"
});


$c.html('').append($b.append( $o.append($i)).append($d));


function getWidth($w){
return parseInt( $w.css('width').replace('px', '') );
}


if (getWidth($o) < getWidth($i))
{
while (getWidth($i) > (getWidth($b) - getWidth($d)) )
{
var content = $i.html();
$i.html(content.substr(0, content.length - 1));
}


$o.css('width', (getWidth($b) - getWidth($d)) + 'px');
}
else
{
var content = $i.html();
$c.empty().html(content);
}
}

It would be called like:

$(function(){
ellipsify($('#test'));
});

I have run into this gremlin over the past week as well.

Since the accepted solution does not account for variable width fonts and wwwhack's solution has a While Loop, I will throw in my $.02.

I was able to drastically reduce the processing time of my problem by using cross-multiplication. Basically, we have a formula that looks like this:

enter image description here

The variable x in this case is what we need to solve. When returned as an Integer, it will give the new length that the over-flowing text should be. I multiplied the MaxLength by 80% to give the ellipses enough room to show.

Here is a full html example:

<html>
<head>
<!-- CSS setting the width of the DIV elements for the table columns.  Assume that these widths could change.  -->
<style type="text/css">
.div1 { overflow: hidden; white-space: nowrap; width: 80px; }
.div2 { overflow: hidden; white-space: nowrap; width: 150px; }
.div3 { overflow: hidden; white-space: nowrap; width: 70px; }
</style>
<!-- Make a call to Google jQuery to run the javascript below.
NOTE:  jQuery is NOT necessary for the ellipses javascript to work; including jQuery to make this example work -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//Loop through each DIV element
$('div').each(function(index) {
var myDiv = this;  //The original Div element which will have a nodeType of 1 (e.g. ELEMENT_NODE)
var divText = myDiv; //Variable used to obtain the text from the DIV element above


//Get the nodeType of 3 (e.g. TEXT_NODE) from the DIV element.  For this example, it will always be the firstChild
divText = divText.firstChild;


//Create another variable to hold the display text
var sDisplayText = divText.nodeValue;


//Determine if the DIV element is longer than it's supposed to be.
if (myDiv.scrollWidth > myDiv.offsetWidth) {
//Percentage Factor is just a way of determining how much text should be removed to append the ellipses
//With variable width fonts, there's no magic number, but 80%, should give you enough room
var percentageFactor = .8;


//This is where the magic happens.
var sliceFactor = ((myDiv.offsetWidth * percentageFactor) * sDisplayText.length) / myDiv.scrollWidth;
sliceFactor = parseInt(sliceFactor);  //Get the value as an Integer
sDisplayText = sDisplayText.slice(0, sliceFactor) + "..."; //Append the ellipses
divText.nodeValue = sDisplayText; //Set the nodeValue of the Display Text
}
});
});
</script>
</head>
<body>
<table border="0">
<tr>
<td><div class="div1">Short Value</div></td>
<td><div class="div2">The quick brown fox jumps over the lazy dog; lots and lots of times</div></td>
<td><div class="div3">Prince</div></td>
</tr>
<tr>
<td><div class="div1">Longer Value</div></td>
<td><div class="div2">For score and seven year ago</div></td>
<td><div class="div3">Brown, James</div></td>
</tr>
<tr>
<td><div class="div1">Even Long Td and Div Value</div></td>
<td><div class="div2">Once upon a time</div></td>
<td><div class="div3">Schwarzenegger, Arnold</div></td>
</tr>
</table>
</body>
</html>

I understand this is a JS only fix, but until Mozilla fixes the bug, I'm just not smart enough to come up with a CSS solution.

This example works best for me because the JS is called every time a grid loads in our application. The column-width for each grid vary and we have no control over what type of computer our Firefox users view our app (which, of course, we shouldn't have that control :) ).

EDIT 09/30/2011

FF7 is out, this bug is resolved and it works!


EDIT 08/29/2011

This issue is marked as resolved and will be available in FF 7; currently set to release on 09/27/2011.

Mark your calendars and get ready to remove all those hacks you've put in place.

OLD

I have another answer: wait.

The FF dev team is in hot pursuit to resolve this issue.

They have tentative fix set for Firefox 6.

Firefox 6!! When will that come out?!?

Easy there, imaginary, over-reactive person. Firefox is on the fast dev track. FF6 is set for release six weeks after Firefox 5. Firefox 5 is set for release June 21st, 2011.

So that puts the fix sometime in the beginning of August 2011...hopefully.

You can sign up for the mailing list following the bug from the link in the original poster's question.

Or you can click here; whichever is easiest.