Javascript 如何在文本中间显示文本溢出省略号
我使用固定宽度的剑道格网。如果文本大于其显示省略号。但是我可以根据字符串的结尾来区分行。由于这种影响,我无法找到它。所以,我需要在正文中间省略。< /P> 例如:Javascript 如何在文本中间显示文本溢出省略号,javascript,css,kendo-ui,Javascript,Css,Kendo Ui,我使用固定宽度的剑道格网。如果文本大于其显示省略号。但是我可以根据字符串的结尾来区分行。由于这种影响,我无法找到它。所以,我需要在正文中间省略。< /P> 例如: abcdefghijklm abcdefg... -> Normal Ellipse abcd...klm -> I Want need this type of output 您添加了css和javascript标记,因此我想到了一个包含两种语言的解决方案 您可以尝试下一种解决方法: 1.在变量_a中减去除最后三个
abcdefghijklm
abcdefg... -> Normal Ellipse
abcd...klm -> I Want need this type of output
您添加了css和javascript标记,因此我想到了一个包含两种语言的解决方案 您可以尝试下一种解决方法:
1.在变量_a中减去除最后三个字符以外的所有字符。在变量中减去最后三个字符。
2.在应用省略号的范围内抛出变量。\br>
3.i在第一个跨距之后立即添加另一个不带省略号的跨距,并用最后三个字符(变量_b)填充它。一种方法如下,当然这依赖于JavaScript:
function centralEllipsis(opts) {
// the default settings, which can be overridden
// by the user:
var settings = {
// the number of the original characters to show:
'maxLength': 7,
// the character-sequence, or HTML character-
// entity to use to replace the missing characters:
'ellipsis': '…',
// the attribute to which you'd like to write the
// original text (this function does also write
// the text to the 'title' attribute though):
'writeToAttribute': 'data-originaltext'
},
// the element upon which we're working (cached
// because we'll access it more than once):
_this = this;
// iterating over the properties supplied by the user:
for (var prop in opts) {
// if the current property ('prop') of the object
// ('opts') is enumerable and not from the prototype:
if (opts.hasOwnProperty(prop)) {
// we update that property in the settings object
// (if the typeof the property-value ('opts[prop]')
// is not equal to the string 'undefined', if it is
// then we use the original property-value:
settings[prop] = 'undefined' !== typeof opts[prop] ? opts[prop] : settings[prop];
}
}
// we divide the settings.maxLength by 2 to work out
// how many characters should appear at the beginning
// of the string; using Math.ceil() to ensure whole
// numbers:
var prefixLength = Math.ceil(settings.maxLength / 2),
// finding the length of the suffix by subtraction
// of the prefixLength from the settings.maxLength:
suffixLength = settings.maxLength - prefixLength,
// setting the textContent of the current element
// as the element's title text and storing it in
// a variable:
originalText = _this.title = _this.textContent,
// empty variables initialised for later:
prefix, suffix;
// if the maxLength is less than the length of the original
// text, then we go ahead (if not, we do nothing):
if (settings.maxLength < originalText.length) {
// storing the original text in the specified attribute:
_this.setAttribute(settings.writeToAttribute, originalText);
// the prefix is the substring of the originalText, for
// settings.maxLength number of characters starting at
// index 0 (the beginning of the string):
prefix = originalText.substr(0, prefixLength);
// if settings.maxLength is less than 2 then the
// suffix is an empty string (''), otherwise it's
// a substring of the originalText, using a negative
// index which takes the last 'suffixLength'
// characters from the string:
suffix = settings.maxLength < 2 ? '' : originalText.substr(-suffixLength);
// here we set the innerHTML (so that we can use HTML
// character-entities, such as '…') to the
// prefix + the settings.ellipsis character(s) + the suffix:
_this.innerHTML = prefix + settings.ellipsis + suffix;
}
}
// Using Function.prototype.call() to use Array.prototype.forEach()
// on the array-like NodeList returned by document.querySelectorAll():
Array.prototype.forEach.call(document.querySelectorAll('.midEllipsis'), function (el) {
// the first argument (here: 'el') supplied to the function
// is the array-element (here a DOM node from the NodeList)
// from the array (NodeList) over which we're iterating.
// using Function.prototype.apply() in order to specify
// that 'this' in the function (centralEllipsis) will be
// the supplied DOM node ('el'); the array is used to
// pass the arguments to the function, the empty object
// is what will be the 'opts' variable in the function
// called. It doesn't have to be there, it's simply left
// in place to show how to pass arguments to the function,
// and how to supply user-defined options to override the
// the defaults:
centralEllipsis.apply(el, [{}]);
});
功能中心化(opts){
//可以覆盖的默认设置
//用户:
变量设置={
//要显示的原始字符数:
“maxLength”:7,
//字符序列,或HTML字符-
//用于替换缺少的字符的实体:
“省略号”:“&hellip;”,
//要向其写入
//原始文本(此函数也编写
//“title”属性的文本:
“writeToAttribute”:“数据原始文本”
},
//我们正在处理的元素(缓存)
//因为我们将多次访问它):
_这个=这个;
//迭代用户提供的属性:
用于(选项中的var prop){
//如果对象的当前属性(“prop”)
//('opts')是可枚举的,而不是来自原型:
如果(选择hasOwnProperty(prop)){
//我们在设置对象中更新该属性
//(如果属性值的类型为('opts[prop]”)
//不等于字符串“undefined”,如果是
//然后我们使用原始属性值:
设置[prop]=“未定义”!==选项类型[prop]?选项[prop]:设置[prop];
}
}
//我们将settings.maxLength除以2算出
//开头应该显示多少个字符
//使用Math.ceil()来确保
//编号:
var prefixLength=Math.ceil(settings.maxLength/2),
//用减法求后缀的长度
//从settings.maxLength中选择的前置桥长度:
后缀长度=设置。最大长度-前缀长度,
//设置当前元素的文本内容
//作为元素的标题文本并将其存储在
//变量:
originalText=\u this.title=\u this.textContent,
//为以后初始化的空变量:
前缀、后缀;
//如果maxLength小于原始文件的长度
//文本,然后我们继续(如果没有,我们什么也不做):
if(settings.maxLength
功能中心化(opts){
变量设置={
“maxLength”:7,
“省略号”:“&hellip;”,
“writeToAttribute”:“数据原始文本”
},
_这个=这个;
用于(选项中的var prop){
如果(选择hasOwnProperty(prop)){
设置[prop]=“未定义”!==选项类型[prop]?选项[prop]:设置[prop];
}
}
var prefixLength=Math.ceil(settings.maxLength/2),
后缀长度=设置。最大长度-前缀长度,
originalText=\u this.title=\u this.textContent,
前缀、后缀;
if(settings.maxLength
- abcdefghijklm