Truncate text with jQuery based on pixel width

前端 未结 5 1893
梦谈多话
梦谈多话 2020-12-08 05:48

I\'m trying to use jquery to write a fast function that calculates the pixel width of a string on a html page, then truncates the string until it reaches an ideal pixel widt

5条回答
  •  爱一瞬间的悲伤
    2020-12-08 06:17

    If you have the text, and know the size you want, why not just use substr?

    I did something similar with

    $('#history-1').text( $('#history-1').text().trim().substr(0,32) + "..." )
    

提交回复
热议问题