问题
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