如何为文本提供readmore?

时间:2016-06-23 05:20:08

标签: javascript html css

我在p标签中有一个长文本。

        <p> 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</P>

如果文本大于100个charectors(如whatsapp中),我想显示更多链接。如何实现? 我是否可以通过pur css来做到这一点? 还是需要js? 它应该在readmore和readless之间切换。

3 个答案:

答案 0 :(得分:1)

尝试以下代码,我认为这对您有所帮助:

HTML:

<p class="minimize">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text.</p>

<p class="minimize">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text.</p>

JS:

jQuery(function(){

    var minimized_elements = $('p.minimize');

    minimized_elements.each(function(){    
        var t = $(this).text();        
        if(t.length < 100) return;

        $(this).html(
            t.slice(0,100)+'<span>... </span><a href="#" class="more">More</a>'+
            '<span style="display:none;">'+ t.slice(100,t.length)+' <a href="#" class="less">Less</a></span>'
        );

    }); 

    $('a.more', minimized_elements).click(function(event){
        event.preventDefault();
        $(this).hide().prev().hide();
        $(this).next().show();        
    });

    $('a.less', minimized_elements).click(function(event){
        event.preventDefault();
        $(this).parent().hide().prev().show().prev().show();    
    });

});

请参阅FIDDLE DEMO

请参阅ReFerance Link

答案 1 :(得分:1)

请参阅此link。我希望它有用。

答案 2 :(得分:0)

试试这个答案

HTMT:

<p id="text"></p>

的javascript:

var text = "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";

var text2 = '';
if(text.length>100){
 var textArray = new Array();
 textArray = text.split("");
 for(var i=0;i<textArray.length;i++){
        text2 += textArray[i];
        if(i==99){
        text2 += "<a id='readmore' href='#more' onclick='document.getElementById(\"more\").style.display=\"inline\";document.getElementById(\"readmore\").style.display=\"none\"'>readmore</a>";
      text2 += "<span name='more' style='display:none' id='more'>";
    }

    if(i==textArray.length-1){
        text2 += "</span>";
    }
 }

}else {
        text2 = text;

}

document.getElementById("text").innerHTML = text2;