JQuery Truncate Text and insert

随声附和 提交于 2021-02-08 10:20:20

问题


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('div[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


回答1:


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



回答2:


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
    }); 
 });


来源:https://stackoverflow.com/questions/5155431/jquery-truncate-text-and-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!