【发布时间】:2013-08-20 10:55:01
【问题描述】:
我尝试剪切过长的文本并用“...”替换它。
HTML:
<div class="test">
<span>Item text goes in here but it is way too long to fit inside a select option that has a fixed width adding more</span>
</div>
jquery:
$.each($('.test span'), function(key, testName) {
var curText = $(testName).text();
$(this).attr('title', curText);
var lengthToShortenTo = Math.round(parseInt($(this).parent('.test').css('width'), 10) / 7.3);
if (curText.length > lengthToShortenTo) {
$(this).text(curText.substring((curText.length - lengthToShortenTo), curText.length) + '... ');
}
});
缩短跨度的输出为:
选择具有固定宽度的选项添加更多...
但我想获取文本的第一部分而不是最后一部分。所以这也是我想要的:
项目文本在此处输入,但太长了...
【问题讨论】:
-
请您更好地解释您想要什么以及您的问题是什么。
-
请给你的问题一个有意义的标题:-)
-
好吧,我希望现在好多了:-)
标签: jquery