开发者

JQuery Truncate Text and insert

I am using some Jquery to insert a element before another element as below:

$(document).ready(function () {
    $('a[href*="FolderCTID"]').each(function () {
        $(this).closest('tr').find('di开发者_如何学运维v[class^="ExternalClass"]').before(this);
    });
});

I am able successfully insert before the element

But, .find('div[class^="ExternalClass"]') in the script above is the element i want to truncate before inserting on runtime.

Please help me, how i can truncate the text before i actually insert

Thanks in advance


var someelement = $(this).closest('tr').find('div[class^="ExternalClass"]');
someelement.html(function(i,h){ return h.substring(0,10); });
someelement.before(this);


Try this:

var truncateLength = 10;
$(document).ready(function(){
    $('a[href*="FolderCTID"]').each(function(){
        var div = $(this).closest('tr').find('div[class^="ExternalClass"]');
        var text = div.text();
        div.text(text.subString(0, text.length > truncateLength  ? truncateLength  : text.length));
        div.insertBefore(this); // insertBefore rather than before
    }); 
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜