更新: text-overflow: ellipsis
现在支持Firefox 7(2011 年 9 月 27 日发布)。耶!我的原始答案作为历史记录如下。
Justin Maxwell 有跨浏览器 CSS 解决方案。但它确实有一个缺点,就是不允许在 Firefox 中选择文本。查看他在 Matt Snider 博客上的客座帖子,了解有关其工作原理的完整详细信息。
innerHTML
请注意,此技术还可以防止使用Firefox中的属性在 JavaScript 中更新节点的内容。请参阅本文末尾的解决方法。
CSS
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
-o-text-overflow: ellipsis;
-moz-binding: url('assets/xml/ellipsis.xml#ellipsis');
}
ellipsis.xml
文件内容
<?xml version="1.0"?>
<bindings
xmlns="http://www.mozilla.org/xbl"
xmlns:xbl="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
>
<binding id="ellipsis">
<content>
<xul:window>
<xul:description crop="end" xbl:inherits="value=xbl:text"><children/></xul:description>
</xul:window>
</content>
</binding>
</bindings>
更新节点内容
要以适用于 Firefox 的方式更新节点的内容,请使用以下命令:
var replaceEllipsis(node, content) {
node.innerHTML = content;
// use your favorite framework to detect the gecko browser
if (YAHOO.env.ua.gecko) {
var pnode = node.parentNode,
newNode = node.cloneNode(true);
pnode.replaceChild(newNode, node);
}
};
请参阅Matt Snider 的帖子以了解其工作原理。