如何添加“阅读更多”链接与jQuery中的很多段落?

问题描述:

我有一个内容DIV与许多段落。 这就是它的标记的外观,如何添加“阅读更多”链接与jQuery中的很多段落?

<div class="more"> 
    <p>Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. </p> 
    <p>That an extinct one-tonne relative of the ncisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. </p> 
    <p>Extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. </p> 
</div> 

现在我需要添加一个链接‘阅读更多’一旦人物达到的<div class="more"> 300。而且我想将此链接显示为切换。

只是我试过用jQuery,但我无法弄清楚它正确。

这是我的jQuery -

var showChar = 300; 
var ellipsestext = "..."; 
var moretext = "more"; 
var lesstext = "less"; 
$('.more').each(function() { 
    var content = $(this).html(); 

    if(content.length > showChar) { 

     var c = content.substr(0, showChar); 
     var h = content.substr(showChar-1, content.length - showChar); 

     var html = c + '<span class="moreelipses">'+ellipsestext+'</span>&nbsp;<span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a href="" class="morelink">'+moretext+'</a></span>'; 

     $(this).html(html); 
    } 

}); 

$(".morelink").click(function(){ 
    if($(this).hasClass("less")) { 
     $(this).removeClass("less"); 
     $(this).html(moretext); 
    } else { 
     $(this).addClass("less"); 
     $(this).html(lesstext); 
    } 
    $(this).parent().prev().toggle(); 
    $(this).prev().toggle(); 
    return false; 
}); 

JS FIDDLE与目前的jQuery

希望有人可以帮助我。谢谢。

+2

你可以看看这个http://jedfoster.com/Readmore.js/ – 2015-02-06 05:47:15

像这样将有助于保持<p>,并有300个字符数。

var showChar = 300; 
 
\t var ellipsestext = "..."; 
 
\t var moretext = "more"; 
 
\t var lesstext = "less"; 
 
\t $('.more').each(function() { 
 
\t var content = $(this).html(); 
 
\t var textcontent = $(this).text(); 
 

 
\t if (textcontent.length > showChar) { 
 

 
\t  var c = textcontent.substr(0, showChar); 
 
\t  //var h = content.substr(showChar-1, content.length - showChar); 
 

 
\t  var html = '<span class="container"><span>' + c + '</span>' + '<span class="moreelipses">' + ellipsestext + '</span></span><span class="morecontent">' + content + '</span>'; 
 

 
\t  $(this).html(html); 
 
     $(this).after('<a href="" class="morelink">' + moretext + '</a>'); 
 
\t } 
 

 
\t }); 
 

 
\t $(".morelink").click(function() { 
 
\t if ($(this).hasClass("less")) { 
 
\t  $(this).removeClass("less"); 
 
\t  $(this).html(moretext); 
 
     $(this).prev().children('.morecontent').fadeToggle(500, function(){ 
 
      $(this).prev().fadeToggle(500); 
 
     }); 
 
     
 
\t } else { 
 
\t  $(this).addClass("less"); 
 
\t  $(this).html(lesstext); 
 
     $(this).prev().children('.container').fadeToggle(500, function(){ 
 
      $(this).next().fadeToggle(500); 
 
     }); 
 
\t } 
 
     //$(this).prev().children().fadeToggle(); 
 
\t //$(this).parent().prev().prev().fadeToggle(500); 
 
\t //$(this).parent().prev().delay(600).fadeToggle(500); 
 
\t 
 
\t return false; 
 
\t });
.morecontent { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="more"> 
 
    <p>Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guins 30cm incisors 
 
    like tusks.</p> 
 
    <p>That an extinct one-tonne relative of the ncisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative guinea picm incisors like tusks.</p> 
 
    <p>Extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks. Based on a study of its skull, scientists say that an extinct one-tonne relative of the guinea pig probably used its 30cm incisors like tusks.</p> 
 
</div>

编辑:代码段更新 - 增加淡入淡出效果与<a href="morelink">外容器

+0

非常感谢您的回答。但有两件事我只需要知道,有没有什么方法可以用这个方法添加'fadeIn()'和'fadeOut()'?我也显示'' from outside of my content DIV? – TNK 2015-02-06 14:55:37

+0

Geat.. Thanks again. One thing How I change this code if '' wrap in a div. Eg. '

' – TNK 2015-02-07 15:50:53

+0

我试着用'find()'和'prev()。prev()'。但我无法实现它的工作。 ().parent()。prev()。children('。morecontent')....' – TNK 2015-02-07 15:53:05

这是你如何acheive JSFIDDLE

var content = $(this).text(); 
//var content = $(this).html(); 

如果浏览器中发现的任何HTML标记突破则其自动与相关标签封闭:</p>

+0

谢谢您的回答。但它显示我的所有段落作为一个... – TNK 2015-02-06 05:44:48

+2

@TNK阅读内容如果你想HTML标签剩余,lenght不是好的解决方案,高度是否适合你的解决方案,就像在这个插件中一样? http://jedfoster.com/Readmore.js/ – 2015-02-06 05:55:54

可能是下面的代码片段将帮助您:

$(document).ready(function(){ 


      var showChar = 300; 
      var ellipsestext = "..."; 
      var moretext = "more"; 
      var lesstext = "less"; 
      var h = ""; 
      var content = $(".more").text(); 

      if(content.length > showChar) 
      { 
       var c = content.substr(0, showChar); 
       h = content.substr(showChar-1, content.length - showChar); 

       var html = c + '<span class="moreelipses">'+ellipsestext+'</span>&nbsp;<span>&nbsp;&nbsp;<a href="" class="morelink">'+moretext+'</a></span>'; 

       $(".more").html(html); 
       $('.morecontent').hide(); 
      } 

     $(".morelink").click(function() 
     { 
      if($(this).hasClass("less")) 
      { 
       $('.more').remove(h); 
       $(this).removeClass("less"); 
       $(this).html(moretext); 
      } 
      else 
      { 
       $('.more').append(h); 
       $(this).addClass("less"); 
       $(this).html(lesstext); 
      } 

      $(this).parent().prev().toggle(); 
      $(this).prev().toggle(); 
      return false; 
     }); 

}); 
+0

你在jsfiddle中测试了这个脚本吗? – 2015-02-06 05:58:34

+0

@jogesh_pi:在我的本地机器上进行了测试。 – 2015-02-06 05:59:48

+0

创建一个小提琴并测试它或编辑用户的小提琴,似乎你的代码中断。 – 2015-02-06 06:00:29