How to trim content of element and put “…” if the characters go over a certain limit?

后端 未结 3 755
甜味超标
甜味超标 2021-01-05 19:04

I would like to trim a part of the if it is too long. This will make sure the table doesn\'t get messed up. All the data in the following table is re

3条回答
  •  半阙折子戏
    2021-01-05 19:29

    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);
        }
    });
    

    Just replace the .artistName selector with the one for your table cell and update the character counts to reflect what you want.

提交回复
热议问题