2015-02-06 48 views
1

我有一個內容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

回答

5

像這樣將有助於保持<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

0

這是你如何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

0

可能是下面的代碼片段將幫助您:

$(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

相關問題