阅读更多脚本

时间:2013-06-04 06:41:25

标签: html

我正在使用一个脚本来限制段落只有几个字,所以对于那个m使用readmore脚本..但问题是它在我点击文本的任何地方都会折叠,而我希望它在点击时折叠>>更多链接..所以我请求任何人帮我解决这个问题,它不会接受点击文字。

                    <!--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>

1 个答案:

答案 0 :(得分: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);
    });