简体   繁体   English

div限制字符中的文本,添加“阅读更多”链接并在单击链接时显示所有字符

[英]Text in div limited characters, add “Read more” link and show all characters when link is clicked

I've a div with text inside that is displayed using PHP & MySQL, the structure is like this: 我有一个带有文本的div,使用PHP和MySQL显示,结构如下:

<div class="description">
    <p>
    Here is a lot of text.
    </p>
</div>

I want to display a "Read more" link when the text inside the p-tag is more than 100 characters. 当p-tag内的文本超过100个字符时,我想显示“阅读更多”链接。 I can display the "Read more" link with PHP like this: 我可以像这样显示PHP的“阅读更多”链接:

// strip tags to avoid breaking any html
$string = strip_tags($string);

if (strlen($string) > 100) {

    // truncate string
    $stringCut = substr($string, 0, 100);

    // make sure it ends in a word so assassinate doesn't become ass...
    $string = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a href="/this/story">Read More</a>'; 
}
echo $string;

The problem is that when the link is clicked I want to show all of the text in the same DIV. 问题是,当点击链接时,我想显示同一个DIV中的所有文本。 Is this possible with PHP or do I need jQuery or something? 这可能是用PHP还是我需要jQuery或其他东西?

If you want to show the full text without a page reload you will have to use javascript/jquery. 如果要在没有页面重新加载的情况下显示全文,则必须使用javascript / jquery。 For that to work the full text has to be in the generated HTML. 要使其工作,全文必须在生成的HTML中。

I did this by using 2 div s, one with the shortened text and one with the full text which is hidden by default. 我通过使用2个div这一点,一个带有缩短的文本,另一个带有默认隐藏的全文。 When 'read more' is clicked I toggle both div s and change the link label to something like 'see less'. 单击“read more”时,我切换两个div并将链接标签更改为“see less”。

You could also put the not-shortened text as well as the ellipsis in an element like so: 您还可以将未缩短的文本以及省略号放在元素中,如下所示:

<div class="readmore">
    This is the shortened text<span class="ellipsis">...</span> <span class="moreText">with the full text hidden</span> <a class="more" href="#">read more</a>
</div>

See this fiddle . 看到这个小提琴

You can do this as 你可以这样做

<script type="text/javascript">
$(document).ready(function(){
    var maxLength = 300;
    $(".show-read-more").each(function(){
        var myStr = $(this).text();
        if($.trim(myStr).length > maxLength){
            var newStr = myStr.substring(0, maxLength);
            var removedStr = myStr.substring(maxLength, $.trim(myStr).length);
            $(this).empty().html(newStr);
            $(this).append(' <a href="javascript:void(0);" class="read-more">read more...</a>');
            $(this).append('<span class="more-text">' + removedStr + '</span>');
        }
    });
    $(".read-more").click(function(){
        $(this).siblings(".more-text").contents().unwrap();
        $(this).remove();
    });
});
</script>
<style type="text/css">
    .show-read-more .more-text{
        display: none;
    }
</style>
<div class="show-read-more">
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur,  from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</div>

Please see this link Show read more link if the text exceeds a certain length using jQuery 如果使用jQuery文本超过一定长度,请参阅此链接显示阅读更多链接

Hope this is what you are looking for. 希望这是你正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM