由于使用css控制文字只显示多行,超出部分显示省略号,存在一定的兼容性问题,所以总结了一下网上一些大咖使用js实现控制行数的解决方案。

第一步:依次引入jquery.js+jquery.ellipsis.js+jquery.ellipsis.unobtrusive.js。

(1)jquery.js源代码下载  http://jquery.com/

(2)jquery.ellipsis.js   源代码

可以通过该部分修改默认的行数,修改row的值即可。

(function($) {
$.fn.ellipsis = function(options) { // default option
var defaults = {
'row' :2, // show rows
'onlyFullWords': false, // set to true to avoid cutting the text in the middle of a word
'char' : '...', // ellipsis
'callback': function() {},
'position': 'tail' // middle, tail
}; options = $.extend(defaults, options); this.each(function() {
// get element text
var $this = $(this);
var text = $this.text();
var origText = text;
var origLength = origText.length;
var origHeight = $this.height(); // get height
$this.text('a');
var lineHeight = parseFloat($this.css("lineHeight"), 10);
var rowHeight = $this.height();
var gapHeight = lineHeight > rowHeight ? (lineHeight - rowHeight) : 0;
var targetHeight = gapHeight * (options.row - 1) + rowHeight * options.row; if (origHeight <= targetHeight) {
$this.text(text);
options.callback.call(this);
return;
} var start = 1, length = 0;
var end = text.length; if(options.position === 'tail') {
while (start < end) { // Binary search for max length
length = Math.ceil((start + end) / 2); $this.text(text.slice(0, length) + options['char']); if ($this.height() <= targetHeight) {
start = length;
} else {
end = length - 1;
}
} text = text.slice(0, start); if (options.onlyFullWords) {
text = text.replace(/[\u00AD\w\uac00-\ud7af]+$/, ''); // remove fragment of the last word together with possible soft-hyphen characters
}
text += options['char']; }else if(options.position === 'middle') { var sliceLength = 0;
while (start < end) { // Binary search for max length
length = Math.ceil((start + end) / 2);
sliceLength = Math.max(origLength - length, 0); $this.text(
origText.slice(0, Math.floor((origLength - sliceLength) / 2)) +
options['char'] +
origText.slice(Math.floor((origLength + sliceLength) / 2), origLength)
); if ($this.height() <= targetHeight) {
start = length;
} else {
end = length - 1;
}
} sliceLength = Math.max(origLength - start, 0);
var head = origText.slice(0, Math.floor((origLength - sliceLength) / 2));
var tail = origText.slice(Math.floor((origLength + sliceLength) / 2), origLength); if (options.onlyFullWords) {
// remove fragment of the last or first word together with possible soft-hyphen characters
head = head.replace(/[\u00AD\w\uac00-\ud7af]+$/, '');
} text = head + options['char'] + tail;
} $this.text(text); options.callback.call(this);
}); return this;
};
}) (jQuery);

(3)jquery.ellipsis.unobtrusive.js源代码

(function ($) {
var $ellipsis = $.fn.ellipsis; $ellipsis.unobtrusive = { parseElement: function (element) {
var $element = $(element);
var maxWidth = $element.data('ellipsis-max-width');
maxWidth = maxWidth ? parseInt(maxWidth) : 0;
var maxLine = $element.data('ellipsis-max-line');
maxLine = maxLine ? parseInt(maxLine) : 1;
$element.ellipsis({ maxWidth: maxWidth, maxLine: maxLine });
}, parse: function (selector) {
$(selector).find("[data-ellipsis=true]").each(function () {
$ellipsis.unobtrusive.parseElement(this);
});
}
}; $(function () {
var beginAt = new Date;
if($ellipsis.debug){
console.log(beginAt);
} $ellipsis.unobtrusive.parse(document); if($ellipsis.debug){
var endAt = new Date;
console.log(endAt);
console.log(endAt - beginAt);
}
});
}(jQuery));

第二步:需要一个装载内容的容器,并在其上添加属性data-toggle="popover",data-ellipsis="true",data-toggle的值可以定义成其他的值,只需后续与调用的js保持一致即可,例如、

 <div style="width:400px">
      <p class="aptitude-title"  data-toggle="popover" data-ellipsis="true">JS控制文字只显示两行,超出部分显示省略号。</p>
</div> 

建议添加一个外层容器,同时添加一个固定宽度。

第三步:调用方法

   $(function () {
$("[data-toggle='popover']").popover();
});

代码搬运,记录过程,便于后续的工作和学习。

JS控制文字只显示两行,超出部分显示省略号的更多相关文章

  1. css文本强制两行超出就显示省略号,不显示省略号

    1. 强制一行的情况很简单 overflow:hidden; //超出的隐藏 text-overflow:ellipsis; //省略号 white-space:nowrap; //强制一行显示 2. ...

  2. css设置一行显示,超出部分显示...

    碰到一些介绍时候需要一行显示,然后超出部分用...替代 white-space: nowrap;不换行显示text-overflow: ellipsis;以...显示overflow: hidden; ...

  3. CSS控制文字只显示一行 超出部分显示省略号

         <p style="width: 120px; height: 50px; border: 1px solid blue; white-space: nowrap; over ...

  4. CSS控制文字只显示一行,超出部分显示省略号

    <p style="width: 300px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;"&g ...

  5. 电力项目七--js控制文字内容过长的显示和文本字数的显示

    当文本框中文字内容过长时,需要调整显示的样式 如上图所示的样式 对应的代码为: <div id="showInfomation" style="visibility ...

  6. div 只显示两行超出部分隐藏

    ; -webkit-box-orient: vertical;line-height: 26px } <td rowspan="2" colspan="2" ...

  7. js控制表格单双行颜色交替显示

    <script language="JavaScript"> window.onload = function() { var Table=document.getEl ...

  8. JS控制文字一个一个出现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. js控制滚动条在内容更新超出时自动滚到底部

    //滚动条在内容更新时自动滚到底部var message = document.getElementById('message');message.scrollTop = message.scroll ...

随机推荐

  1. 在Fragment中加一个嵌套了ListView的ScrollView(一)

    首先介绍一下这个程序的功能: 1.顶部有两个可以切换Fragment的Button 2.在其中一个Fragment中里有个ScrollView,ScrollView中有ViewFlipper,List ...

  2. Objective C - 4 - 下载图片并且加载到View

    #import "LoadInternetImageViewController.h" @interface LoadInternetImageViewController () ...

  3. python类中self是什么

    参考文献:http://www.cnblogs.com/linuxcat/archive/2012/01/05/2220997.html 注: (1)self在定义类的方法时是必须有的. (2)调用时 ...

  4. OC中使用单例模式

    static Config * instance = nil; +(Config *) Instance { @synchronized(self) { if(nil == instance) { [ ...

  5. Python 实现windows后台服务

    # -*- coding: utf-8 -*- import sys import win32api import win32con import win32event import win32ser ...

  6. HDU1520 Anniversary party 树形DP基础

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  7. a链接嵌套无效,嵌套链接最优解决办法

    <a>不支持嵌套.例如: <a href="#1">11111111111<a href="#2">22222222222& ...

  8. 将 WPF、UWP 以及其他各种类型的旧样式的 csproj 文件迁移成新样式的 csproj 文件

    写过 .NET Standard 类库或者 .NET Core 程序的你一定非常喜欢微软为他们新开发的项目文件(对于 C#,则是 csproj 文件).这种文件非常简洁,组织一个庞大的项目也只需要聊聊 ...

  9. Cockpit 服务化管理工具

    Cockpit 是红帽开发的网页版图像化服务管理工具,优点是无需中间层,且可以管理多种服务. 根据其项目主站描述,Cockpit 有如下特点: 从易用性考虑设计,方便管理人员使用,而不是仅仅的终端命令 ...

  10. selectToUISlider

    版权声明:本文为LZUGIS原创文章,未经同意不得转载. https://blog.csdn.net/GISShiXiSheng/article/details/24304321 用过google e ...