使用高度和宽度添加省略号

时间:2014-06-25 16:21:28

标签: javascript jquery css

我正在尝试使用带有the below code的javascript在多行上添加省略号。它适用于大多数情况,除了一种情况,其中文本宽度与单元格的宽度相同。在那种情况下,我无法添加省略号。所以我也需要检查宽度。

while ($(this).height() > 60) {
  $(this).text(function (index, text) {
    return text.replace(/\W*\s(\S)*$/, '...');
  });
}   

1 个答案:

答案 0 :(得分:0)

尝试使用overflow:hidden的容器将省略号添加到溢出的内部段落标记中。

var $container = $(container);
    var $p = $container.find("p");
    var divh = $container.height();

    while ($p.outerHeight()>divh) {
        $p.text(function (index, text) {
            return text.replace(/\W*\s(\S)*$/, '...');
         });
    }
相关问题