jquery - 如何用dotdotdot实现多读和少读?

标签 jquery dotdotdot

使用 jQuery dotdotdot插件,我想要有一个更多更少按钮来显示和隐藏 <div> 的全部内容当有大量文本需要显示时。 更多按钮工作得很好,但我还没有找到返回 <div> 的方法。到原来的显示。请注意,这不仅仅是关于如何使用 dotdotdot 来扩展截断的字符串,因为它包含了重新截断长字符串的 Less 按钮。

这里是my code :

$(function() {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").find("a").click(function() {
        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy').find('a.more').hide();
            div.css('max-height', '');
            $("a.less", div).show();
        }
        else {
            $(this).text("More");
            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a", callback: dotdotdotCallback });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
         $("a", this).remove();   
        }
    }
});

看来click <div> 的事件处理程序的 anchor 标记被删除,单击更多按钮后我永远无法到达事件处理程序。

找到解决方案:

Updated code :

$(function() {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").on('click','a',function() {
        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy').find('a.more').hide();
            div.css('max-height', '');
            $("a.less", div).show();
        }
        else {
            $(this).hide();
            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({ after: "a.more", callback: dotdotdotCallback });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
         $("a", this).remove();   
        }
    }
});

谢谢大家!

最佳答案

更改此:

$("div.ellipsis-text").find("a").click(function() {

对此:

$("div.ellipsis-text").on('click','a',function() {

更新的代码:

$(function () {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").on('click', 'a', function () {

        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy').find('a.more').hide();
            div.css('max-height', '');
            $("a.less", div).show();
        } else {

            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({
                after: "a",
                callback: dotdotdotCallback
            });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
            $("a", this).remove();
        }
    }
});

第二种方法是使用单个 a 标签并以这种方式切换其文本:

HTML:

    <div class='ellipsis-text'>"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" 

<a class='more' href='#'>More</a>

 </div>

JQUERY:

$(function () {
    $("div.ellipsis-text").dotdotdot({
        after: 'a.more',
        callback: dotdotdotCallback
    });
    $("div.ellipsis-text").on('click', 'a', function () {

        if ($(this).text() == "More") {
            var div = $(this).closest('div.ellipsis-text');
            div.trigger('destroy')
            div.css('max-height', '');
            $(this).text("Less");

        } else {
            $(this).text("More")
            $(this).closest('div.ellipsis-text').css("max-height", "50px").dotdotdot({
                after: "a",
                callback: dotdotdotCallback
            });
        }
    });

    function dotdotdotCallback(isTruncated, originalContent) {
        if (!isTruncated) {
            $("a", this).remove();
        }
    }
});

关于jquery - 如何用dotdotdot实现多读和少读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187774/

相关文章:

javascript - 从哪里导入js文件比较好?

javascript - 根据检查和检查其他复选框调用函数

javascript - JSON AJAX 请求在使用方括号时返回未定义

网格布局上的 jQuery.dotdotdot -> 空白区域

javascript - 多行文本溢出(dotdotdot): wrap only word

javascript - 如何使用 dotdotdot 展开 chop 的文本 onclick?

javascript - jQuery、insertBefore、验证、错误消息的放置

javascript - 如何仅使用 jquery 一次在导航中添加 nav-active 类

jquery.dotdotdot 部分未显示

jquery - chrome 中带省略号的多行文本问题