2013-06-04 107 views
1

我正在使用一個腳本來限制段落只有幾個字,所以對於使用readmore腳本的m來說。但問題是它會摺疊段落,無論我點擊文本而我希望它被摺疊當點擊>>更多鏈接..因此我要求任何人來幫助我這個做什麼,它不會接受點擊文本。閱讀更多腳本

    <!--readmore script--> 
       <script type="text/javascript"> 

       $(function() { 

        // Grab all the excerpt class 
        $('.excerpt').each(function() { 

         // Run formatWord function and specify the length of words display to viewer 
         $(this).html(formatWords($(this).html(), 15)); 

         // Hide the extra words 
         $(this).children('span').hide(); 

        // Apply click event to read more link 
        }).click(function() { 

         // Grab the hidden span and anchor 
         var more_text = $(this).children('span.more_text'); 
         var more_link = $(this).children('a.more_link'); 

         // Toggle visibility using hasClass 
         // I know you can use is(':visible') but it doesn't work in IE8 somehow... 
         if (more_text.hasClass('hide')) { 
          more_text.show(); 
          more_link.html(' &raquo; hide');   
          more_text.removeClass('hide'); 
         } else { 
          more_text.hide(); 
          more_link.html(' &laquo; more');    
          more_text.addClass('hide'); 
         } 

         return false; 

        }); 
       }); 

       // Accept a paragraph and return a formatted paragraph with additional html tags 
       function formatWords(sentence, show) { 

        // split all the words and store it in an array 
        var words = sentence.split(' '); 
        var new_sentence = ''; 

        // loop through each word 
        for (i = 0; i < words.length; i++) { 

         // process words that will visible to viewer 
         if (i <= show) { 
          new_sentence += words[i] + ' '; 

         // process the rest of the words 
         } else { 

          // add a span at start 
          if (i == (show + 1)) new_sentence += ' <span class="more_text hide">';  

          new_sentence += words[i] + ' '; 

          // close the span tag and add read more link in the very end 
          if (words[i+1] == null) new_sentence += '</span><a href="#" class="more_link"> &raquo; more</a>'; 
         }  
        } 

        return new_sentence; 

       } 
       </script> 
        <!--readmore script--> 


<body> 
<p class="excerpt">Lorem Ipsum is simply dummy text of the printing and typesetting  industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of 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.<br><br> 


    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 here, 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, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)....</p> 
</body> 
+0

沒有人會通過找。嘗試並削減它,也許只是功能的開始。 – Mattios550

回答

2

這些小腳本怎麼樣? DEMO http://jsfiddle.net/uEXvk/

HTML

<p class="excerpt">Lorem Ipsum is simply dummy text of the printing and typesetting  industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of 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.<br><br> 


    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 here, 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, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)....</p> 

CSS

a { 
    color: blue; 
    cursor: pointer; 
} 

JQUERY

var orgContent = $('.excerpt').html(); 
    var txtContent = $('.excerpt').text().substr(0, 50) + '... <a id="morelink">more</a>'; 
    $('.excerpt').html(txtContent); 
    $("body").on("click", '#morelink', function(){ 
     $('.excerpt').html(orgContent); 
     $('<a id="lesslink"> less</a>').appendTo('.excerpt'); 
    }); 
    $("body").on("click", '#lesslink', function(){ 
     $('.excerpt').html(txtContent); 
    }); 
+0

是的,先生,它工作正常..謝謝你這麼多的幫助先生:) –

+1

不客氣,那麼綠色的勾號呢? ;) – yeyene

+0

哦,是的,先生,你值得這個..請繼續幫助我先生thx噸:) –