如何裁剪超出元素长度的字符串?

3 html javascript jquery

在我的html页面中,我有工具提示.由于工具提示图像中的长度,某些字符串无法正确拟合.但是,我不允许从服务器端裁剪字符串.

我想让ToolTip显示前8-12个字符,然后是点.

在javascript或jquery中是否有任何内置函数可用.

谢谢!

zsa*_*ank 13

您可以使用string.substring(from, to):http://www.w3schools.com/jsref/jsref_substring.asp

然后只需追加 "..."

var x = "a really long line of text";
var toolTipText = x.substring(0, 8) + "...";
Run Code Online (Sandbox Code Playgroud)


Abd*_*aly 11

tip = tip.length > 12 ? tip.substring(0,12) + "..." : tip;
Run Code Online (Sandbox Code Playgroud)