最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to expand truncated text onclick by using dotdotdot? - Stack Overflow

programmeradmin0浏览0评论

I am using the jQuery dotdotdot truncation plugin dotdotdot.frebsite.nl

I want to truncate at max 2 lines. And when a user clicks on more, then it must show the full text (expand/de-truncate).

So far, I "only" manage to truncate my text. But not to "de-truncate" it.

Here is my code:

<p class="truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vitae tellus eu dui placerat interdum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.<a class="read-more" href="#">more</a></p>

$(document).ready(function(){

    $('.truncate').dotdotdot({
        ellipsis     : '… ',
        watch        : true,
        wrap         : 'letter',
        height       : parseInt( $('.truncate').css('line-height'), 10) * 2, // this is the number of lines
        lastCharacter: {
            remove: [ ' ', ',', ';', '.', '!', '?' ],
            noEllipsis: []
        },
        after: "a.read-more"
    });


});

Live demo jsfiddle/NSnxe/1

I am using the jQuery dotdotdot truncation plugin dotdotdot.frebsite.nl

I want to truncate at max 2 lines. And when a user clicks on more, then it must show the full text (expand/de-truncate).

So far, I "only" manage to truncate my text. But not to "de-truncate" it.

Here is my code:

<p class="truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vitae tellus eu dui placerat interdum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.<a class="read-more" href="#">more</a></p>

$(document).ready(function(){

    $('.truncate').dotdotdot({
        ellipsis     : '… ',
        watch        : true,
        wrap         : 'letter',
        height       : parseInt( $('.truncate').css('line-height'), 10) * 2, // this is the number of lines
        lastCharacter: {
            remove: [ ' ', ',', ';', '.', '!', '?' ],
            noEllipsis: []
        },
        after: "a.read-more"
    });


});

Live demo jsfiddle/NSnxe/1

Share Improve this question edited Mar 18, 2015 at 11:59 kenorb 167k95 gold badges708 silver badges779 bronze badges asked Sep 25, 2013 at 22:03 cusejuicecusejuice 10.7k27 gold badges95 silver badges150 bronze badges 5
  • What is your question? – Barbara Laird Commented Sep 25, 2013 at 22:07
  • When a user clicks "more," I want the full text to expand and 'de-truncate'. – cusejuice Commented Sep 25, 2013 at 22:09
  • Note that the values you have for lastCharacter option are the same as the default ones. Hence this part of the code is irrelevant. – Adriano Commented Mar 17, 2015 at 14:40
  • possible duplicate of Read More and Read Less with dotdotdot jQuery – Adriano Commented Mar 18, 2015 at 9:56
  • I added a plete answer to the same question on stackoverflow./questions/25187774/… – Adriano Commented Mar 18, 2015 at 9:57
Add a ment  | 

1 Answer 1

Reset to default 6

You can send a destroy message to dotdotdot

$('a.read-more').on( 'click', function(event) {
    event.preventDefault();
    $(this).parent().trigger("destroy");
});

http://jsfiddle/bhlaird/C5Ent/

发布评论

评论列表(0)

  1. 暂无评论