長すぎるテキストを切り取って「...」に置き換えようとしています。
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>
Jクエリ:
$.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) + '... ');
}
});
短縮されたスパンの出力は次のとおりです。
さらに追加する固定幅のオプションを選択...
しかし、テキストの最後の部分ではなく、最初の部分を取得したいのです。だから、これも私が欲しいものです:
ここにアイテムのテキストが入りますが、長すぎます...