【问题标题】:How to implement Read More and Read Less with dotdotdot?如何用dotdotdot实现Read More和Read Less?
【发布时间】:2023-03-13 12:46:01
【问题描述】:

使用 jQuery dotdotdot 插件,我希望有一个 MoreLess 按钮来显示和隐藏 <div> 的全部内容很多文字要显示。 More 按钮工作正常,但我还没有找到将<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();   
        }
    }
});

似乎<div> 的锚标记的click 事件处理程序正在被删除,单击更多按钮后我永远无法访问事件处理程序。 p>

找到的解决方案:

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

谢谢各位!

【问题讨论】:

标签: jquery dotdotdot


【解决方案1】:

改变这个:

$("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();
        }
    }
});

【讨论】:

    【解决方案2】:

    我参加这个聚会有点晚了,但我想你可能会喜欢这个代码。

    同样在jsfiddle.net/fo4z05qp

    var bindReadMore = function(){
        $('.read-more').on('click', function(e) {
            e.preventDefault();
            var parent = $(this).parent();
            parent.trigger("destroy");
            parent.removeClass('truncable-txt--is-truncated');
            parent.addClass('truncable-txt--is-not-truncated');
            bindReadLess(); // bind click on "less"
        });
    };
    
    var bindReadLess = function(){
        $('.read-less').on('click', function(e) {
            e.preventDefault();
            var parent = $(this).parent();
            truncateIfNeeded(parent); // Re-initialize ellipsis
        });
    };
    
    var truncateIfNeeded = function(jqueryTag){
        var $selectionToTruncate = jqueryTag || $('.truncable-txt');
        
        $selectionToTruncate.dotdotdot({
            ellipsis: '...',
            watch   : true,
            wrap    : 'letter',
            height  : 20 * 3, // max number of lines
            after   : '.read-more',
            callback: function( isTruncated, orgContent ) {
                var $currentReadMore = $(this).find('.read-more');
                var $currentReadLess = $(this).find('.read-less');
                
                if( isTruncated ){
                    $(this).addClass('truncable-txt--is-truncated');
                }
                bindReadMore(); // bind click on "read more"
            },
        });
    };
    
    $(function() {
        truncateIfNeeded(); // Initialize ellipsis
    });
    .trigger-js {
        cursor: pointer;
        cursor: hand;
    }
    .read-less,
    .read-more {
        display: none;
    }
    
    .read-more__txt,
    .read-less__txt {
        text-decoration: underline;
    }
    
    /* [1] downwards arrow, fileformat.info/info/unicode/char/2193/index.htm */
    .read-more::after {
        margin-left: 4px;
        content: '\2193'; /* [1] */
    }
    /* [2] upwards arrow, fileformat.info/info/unicode/char/2191/index.htm */
    .read-less::after {
        margin-left: 4px;
        content: '\2191'; /* [2] */
    }
    
    .truncable-txt--is-truncated .read-more,
    .truncable-txt--is-not-truncated .read-less {
        display: inline-block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://rawgit.com/BeSite/jQuery.dotdotdot/master/src/js/jquery.dotdotdot.min.js"></script>
    <p class="truncable-txt">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
        <span class="read-more trigger-js">&nbsp;<a class="read-more__txt">more</a></span>
        <span class="read-less trigger-js">&nbsp;<a class="read-less__txt">less</a></span>
    </p>
    
    <p class="truncable-txt">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
        <span class="read-more trigger-js">&nbsp;<a class="read-more__txt">more</a></span>
        <span class="read-less trigger-js">&nbsp;<a class="read-less__txt">less</a></span>
    </p>

    资源

    【讨论】:

      【解决方案3】:

      Ehsan Sajjad 的回答是正确的,但您也有一个错误,您从一个更多和一个更少按钮开始,但将“更少”按钮重命名为“更多”而不是隐藏它。我将 Fiddle 上的 JS 修改为这个,现在它似乎可以正常工作了。

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

      【讨论】:

      • 是的,我也看到了,但我不知道我的电脑发生了什么,我无法保存和更新任何小提琴,问题是更新和保存按钮在我的电脑
      • @EhsanSajjad 90% 确定这是您的问题的解决方案:不要使用 Internet Explorer ;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多