firefox css 省略号,检测CSS文本溢出:Firefox中的省略号

其实我写了一个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('

}

$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();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值