jquery在jquery进程中使用p类时如何限制字符?

jquery在jquery进程中使用p类时如何限制字符?,jquery,Jquery,这是我的代码,我想根据类限制段落中的字符,但它不起作用,因为我在jquery过程中创建了p类 $('.go-back').click(function(){ if($('.item').hasClass('active')){ var current =''; current += '<p class="descSub">'+$('.active').find('.title').html()+'</p>'; /

这是我的代码,我想根据类限制段落中的字符,但它不起作用,因为我在jquery过程中创建了p类

$('.go-back').click(function(){
     if($('.item').hasClass('active')){
        var current ='';
        current += '<p class="descSub">'+$('.active').find('.title').html()+'</p>';

        // code to limit characters of p class above 
        var titleLength = $('.descSub');
        titleLength.text(titleLength.text().substring(0,20));
        $('.content').html(current);
     }
});

如果我理解正确,只需更改命令字符限制的顺序。在您的示例中,您是在附加了它之后才执行它的,您所需要做的就是在附加之前执行它

 $('.go-back').click(function(){
 if($('.item').hasClass('active')){

    var current ='';
    //grab title from active .title first
    var titleLength = $('.active').find('.title').html();
    //then crop to 20 chars max
    titleLength = titleLength.substring(0,20);
    //then add to current
    current += '<p class="descSub">'+ titleLength +'</p>';
    //then put to .contents html
    $('.content').html(current);
 }

});

在这里拉小提琴,这样您就可以看到它的作用:

谢谢,我认为还是不走运,没有得到任何内容,而且当我放置alertcurrent时,它也不会显示警报弹出窗口。@den抱歉,我没有检查子字符串函数-它的格式不正确。我帮你修好了,现在检查代码。也为你增加了小提琴!