Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery截断文本并插入_Javascript_Jquery - Fatal编程技术网

Javascript JQuery截断文本并插入

Javascript JQuery截断文本并插入,javascript,jquery,Javascript,Jquery,我使用一些Jquery在另一个元素之前插入一个元素,如下所示: $(document).ready(function () { $('a[href*="FolderCTID"]').each(function () { $(this).closest('tr').find('div[class^="ExternalClass"]').before(this); }); }); 我能够成功地在元素之前插入 但是,上面脚本中的.find('div[class^=“E

我使用一些Jquery在另一个元素之前插入一个元素,如下所示:

$(document).ready(function () {
    $('a[href*="FolderCTID"]').each(function () {
        $(this).closest('tr').find('div[class^="ExternalClass"]').before(this);
    });
});
我能够成功地在元素之前插入

但是,上面脚本中的.find('div[class^=“ExternalClass”]”)是我在运行时插入之前要截断的元素

请帮助我,如何在实际插入之前截断文本

提前感谢

试试这个:

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

您要执行什么样的截断?到特定的长度?在一个特定的地方?@justkt我试图截断字符的特定长度,脚本不会返回任何关于这个的信息?我在你的另一个问题中使用了我的答案中的脚本:对不起,脚本返回nothing@Jam-更新为使用insertBefore,而不是以前。你现在能测试一下吗?
var someelement = $(this).closest('tr').find('div[class^="ExternalClass"]');
someelement.html(function(i,h){ return h.substring(0,10); });
someelement.before(this);