如何修剪元素的内容并放置"..."字符是否超过一定限制? [英] How to trim content of element and put "..." if the characters go over a certain limit?
问题描述
如果<td>
太长,我想修剪它的一部分.这将确保表格不会被弄乱.下表中的所有数据都是从数据库中检索的.在主题"部分,如果超出某个字符限制,我该如何缩短文本并放入"..."?这是表格的屏幕截图:
I would like to trim a part of the <td>
if it is too long. This will make sure the table doesn't get messed up. All the data in the following table is retrieved from a database. On the "Subject" part, how could I make the text shorten and put "..." if it goes over a certain character limit? Here is a screenshot of the table:
如您所见,如果主体时间太长,可能会弄乱桌子.
As you can see if the subject gets too long it may mess up the table.
是否有任何jQuery会在元素超过一定限制并在末尾加上"..."时对元素进行修剪?我认为截断插件不会起作用,因为它通常会将其拆分并换行,并且我不需要显示它,因为使用该插件的所有人都必须单击查看票证",他们会看到那里的全部主题.
Is there any jQuery that will make the element trim only if it goes over a certain limit and put "..." at the end? I don't think a truncate plugin would work because it usually splits it up and makes a line break and I don't need it to show because all the person using this would have to do is click "View ticket" and they will see the full subject there.
我在StackOverflow上搜索了其他问题,但找不到我需要的问题.
I searched other questions on StackOverflow but I couldn't find one that is what I need.
推荐答案
这是我用来查看歌手姓名是否超过33个字符的小片段
Here is a little snippet that I used to see if an artists name was over 33 characters
// Elipses
$('.artistName').each(function() {
var that = $(this),
title = that.text(),
chars = title.length;
if (chars > 33) {
var newTitle = title.substring(0, 30) + "...";
that.text(newTitle);
}
});
只需将.artistName选择器替换为表格单元格的选择器,然后更新字符数以反映您想要的内容.
Just replace the .artistName selector with the one for your table cell and update the character counts to reflect what you want.
这篇关于如何修剪元素的内容并放置"..."字符是否超过一定限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!