【问题标题】:How do I add a read more link at the end of a paragraph?如何在段落末尾添加阅读更多链接?
【发布时间】:2013-04-17 11:11:42
【问题描述】:

我正在尝试在段落末尾添加一个链接以阅读更多文本。我希望这个链接在最后的段落中显示,如下所示:

我希望段落附有 ... 和阅读更多链接。

现在我不希望阅读更多链接做任何事情,因为一旦我获得链接,我将添加我自己的功能。我只是在努力寻找一种方法让链接看起来像这样。

我一直在查看以下 jquery 脚本,但这似乎适用于字符数,并在任何字符限制处剪切最后一个单词,然后不显示我想要的方式(链接出现在下面这段落)。它还已经包含了单击链接时发生的情况的功能,由于我缺乏 jquery 的能力,我一直未能成功编辑。

$(function(){ /* to make sure the script runs after page load */
    $('.customer_experience').each(function(event){ /* select all divs with the  customer_experience class */
        var max_length = 250; /* set the max content length before a read more link will be added */

        if($(this).html().length > max_length){ /* check for content length */
            var short_content   = $(this).html().substr(0,max_length); /* split the content in two parts */
            var long_content    = $(this).html().substr(max_length);

            $(this).html(short_content+
                '<a href="#" class="read_more"><br/>read more...</a>'+
                '<span class="more_text" style="display:none;">'+long_content+'</span>'); /* Alter the html to allow the read more functionality */

            $(this).find('a.read_more').click(function(event){ /* find the a.read_more element within the new html and bind the following code to it */
                event.preventDefault(); /* prevent the a from changing the url */
                $(this).parents('.customer_experience').find('.more_text').show(); /* show the .more_text span */
            });
        }
    });
});

我是否需要使用 jQuery 来获得我正在寻找的结果?这可以单独使用 CSS 完成吗?我根本不会写 jQuery,所以我有点迷茫。

任何关于我可以去哪里的帮助或提示将不胜感激。

已编辑以添加 HTML 标记

<div class='comments_container'>
    <img src='images/comment-icon.png'/>
    <h1 class='comments_title'>Customer experience</h1>
    <p class='customer_experience'>$customer_exp_one</p>
</div>

有问题的段落是.customer_experience

【问题讨论】:

  • 你能分享你的html标记吗?
  • @AlexLynham 是的。但是,它是由php驱动的。
  • 您希望链接在达到字符数之前、在达到字数之后还是在内容超过指定宽度/高度的块之后出现在最后一个单词之后?
  • 您要减少段落还是字数?
  • @DerekHenderson 理想情况下它会超过包含 div 的高度。

标签: jquery css


【解决方案1】:

希望下面的内容对你有所帮助。

http://jsfiddle.net/X5r8r/1156/

body, input {
    font-family: Calibri, Arial;
    margin: 0px;
    padding: 0px;
}
a {
    color: #0254EB
}
a:visited {
    color: #0254EB
}
#header h2 {
    color: white;
    background-color: #00A1E6;
    margin: 0px;
    padding: 5px;
}
.comment {
    width: 400px;
    background-color: #f0f0f0;
    margin: 10px;
}
a.morelink {
    text-decoration: none;
    outline: none;
}
.morecontent span {
    display: none;
}

【讨论】:

  • 这很有帮助。谢谢你。我不希望 div 在单击更多链接时展开。这可以从jquery中编辑出来吗?我只想显示链接,以便我可以向其中添加自己的功能。我要添加一个jquery弹出窗口来完整显示文本。
  • 嗨,Richard Bell,我更新了小提琴,请查看jsfiddle.net/X5r8r/1158
  • 太棒了!你的回答最符合我的要求。我将尝试将 lordvlad 提出的解决方案与您的解决方案结合起来,这样结束词就不会被删减。非常感谢您的时间和精力。
  • @Richard Bell,我注意到如果阅读更多部分中的文本包含其他元素,这将不起作用。有什么好办法解决这个问题吗?
【解决方案2】:

用于截断段落。此脚本检查段落数,而不是字数。

JS(使用 Jquery)

$(document).ready(function() {

    /* Count of paragraphs shown */
    var cutCount = 2;

    $("#testID p").each(function (i) {
        i++;
        if(i == cutCount) {
            $(this).append(' <a href="javascript:void(1)" onclick="$(\'#testID p\').show(); $(this).hide()">Read more</b>')   
        }
        if(i > cutCount) {
           $(this).hide();
        }
    });

});

HTML

<div id="testID">

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam egestas lobortis mi, quis convallis arcu interdum id. Nunc velit justo, congue ac vestibulum eu, sodales sed nulla. Nam semper egestas nunc, placerat suscipit lorem fringilla sit amet. Etiam id metus quis tellus luctus rhoncu</p>

<p>Donec euismod, dui aliquam vestibulum rutrum, urna ipsum luctus justo, in eleifend tellus ligula et elit. Morbi gravida lacinia magna quis faucibus. Duis arcu ligula, euismod sed ornare quis, posuere ac quam. Sed orci metus, pretium ut blandit sed, ullamcorper in eros. Mauris at euismod diam. Quisque auctor congue erat et varius. Nulla odio orci, convallis non fringilla vel.</p>

<p>Curabitur a velit eu lacus vestibulum vehicula. Proin mi velit, tempor quis convallis vulputate, adip</p>

<p>Nullam eget pulvinar arcu. Nam tellus tortor, luctus non rutrum eget, condimentum ac lectus. Nulla diam tellus, aliquet non auctor nec, bibendum nec orci. Proin rhoncus sodales sapien, sit amet eleifend erat sagittis quis. Ut et urna et erat venenatis convallis at sit amet sem. Quisque tempus sodales ornare. Maecenas eget nisi et ligula facilisis dictum.</p>

<p>Etiam lectus dolor, tincidunt a ultricies et, vehicula ut lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean bibendum suscipit nunc sit amet ultrices. Praesent nunc velit, dignissim eget tincidunt non, varius at velit. Aliquam ac interdum lectus. Nulla a ante lacinia arcu suscipit suscipit a nec mauris. In sed tempor massa. Donec luctus dapibus dolor, vel mattis justo pulvinar eget. Pellentesque scelerisque dolor ut erat cursus ornare. In in nunc et tellus consequat convallis id sed est. Integer dictum fermentum tempus. Ut lobortis ante vel ante condimentum lacinia. Donec malesuada pretium sapien, et euismod turpis faucibus at. Duis vel turpis ultrices enim viverra vestibulum vitae sed sapien.</p>

</div>

示例 http://jsfiddle.net/9bXQh/2/

【讨论】:

  • 这很完美,但是如何在展开时获得少读按钮?
【解决方案3】:

你可以用这样的东西来切割空间:

$(this).html().substr(0, $(this).html().indexOf(" ", max_length))

【讨论】:

  • 所以这会在达到字符数之前在最后一个空格处剪切它?
  • @RichardBell,这实际上会在达到字符数后在第一个空格处剪切。
  • 好吧,听起来不错。我会稍微减少计数。我现在要玩它。谢谢。
  • 最好使用 trim。 $.trim(" hello, how are you? "); 变为 hello, how are you?
  • 谢谢,这正是我需要集成到我更复杂的新闻幻灯片插件中。
【解决方案4】:

我进行了网络搜索,发现了这个插件:http://dotdotdot.frebsite.nl/

我想这并不能真正回答您的问题,只是为您指明可能的解决方案。看来这个插件可以让你做你想做的事:当文本超过一个固定高度的容器时截断文本,添加一个“阅读更多”链接,并将你自己的自定义函数绑定到“阅读更多”链接。

【讨论】:

  • 有趣的解决方案。谢谢,我会调查的。
【解决方案5】:

单击下面的按钮可通过类更改显示和隐藏另一个元素:

.collapse 隐藏内容

.collapsing 在转换期间应用

.collapse.show 显示内容 您可以使用带有href 属性的链接,或带有data-target 属性的按钮。在这两种情况下,data-toggle="collapse" 都是必需的。 HTML

  <div class="container">

     <div class="row justify-content-center">
      <div class="col-md-4">
        <div id="parent">
         <p class="collapse" id="collapseParent">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc     </p>
         <a class="collapsed" data-toggle="collapse" href="#collapseParent" aria-expanded="false" aria-controls="collapseParent"></a>
   </div>
 </div> 

CSS

#parent {
   font-size: 14px;
   line-height: 1.5;
}

#parent p.collapse:not(.show) {
   height: 42px !important;
   overflow: hidden;
   display: -webkit-box;
   -webkit-line-clamp: 2;
   -webkit-box-orient: vertical;  
  }

#parent p {
    min-height: 42px !important;
}

 #parent a.collapsed:after  {
     content: 'Read More';
  }

  #parent a:not(.collapsed):after {
      content: 'Read Less';
  }

CodePen Example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 2021-09-20
    • 1970-01-01
    • 2011-10-30
    • 2016-09-28
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多