1

長すぎるテキストを切り取って「...」に置き換えようとしています。

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) + '... ');
    }
});

短縮されたスパンの出力は次のとおりです。

さらに追加する固定幅のオプションを選択...

しかし、テキストの最後の部分ではなく、最初の部分を取得したいのです。だから、これも私が欲しいものです:

ここにアイテムのテキストが入りますが、長すぎます...

デモ: http://jsfiddle.net/jNWS6/247/

4

3 に答える 3

1

この行を変更するだけでいいと思います

$(this).text(curText.substring((curText.length - lengthToShortenTo), curText.length) + '... ');

これに

$(this).text(curText.substring(0, lengthToShortenTo) + '... ');

R.

于 2013-08-18T15:11:26.520 に答える
1

部分文字列の間違った部分を取っています。最初の部分を取る必要があります

$.each($('.sedcardHolder span'), function(key, sedcardName) {
    var curText = $(sedcardName).text();
    $(this).attr('title', curText);

    var lengthToShortenTo =     Math.round(parseInt($(this).parent('.sedcardHolder').css('width'), 10) / 5.3);

    if (curText.length > lengthToShortenTo) {
        $(this).text(curText.substring(0,(curText.length - lengthToShortenTo)) + '... ');
    }
});
于 2013-08-18T15:17:10.383 に答える