0

我正在尝试创建一个自动阅读更多和阅读更少的内容,您只需在<a href="#" class="readmore">YOUR TEXT"</a>文本中的任何地方弹出并遇到问题。现在它几乎可以工作,但如果你打开几个不同的阅读器然后点击阅读器关闭页面上的所有阅读器。有任何想法吗?所有代码都在下面,并带有注释。

// Auto Readmore with a link that has the readmore class
$(document).ready(function() {
    // Show Read more link that is hidden by css
    $('.readmore').show("fast");
    // Loop through all  .readmores
    $('a.readmore').each(function(index) {
        // Set the Node of the link
        var currentreadmore = $(this);
        // set the id to the index of the each
        $(this).attr('id', 'readmore-' + index);
        var node = currentreadmore[0];
        if (node && node.nextSibling) {
            // if not at the end of a paragraph remove the rest of the paragraph and append ...
            var afternodecontent = node.nextSibling.nodeValue;
            node.nextSibling.nodeValue = " ...";
        }
        // hide everything after the readmore link
        $(this).parent().nextAll().hide();

        // create click function to append the rest of the paragraph and show the content. Also hides the readmore button
        $(this).click(function() {
            // add the content back in
            if (node && node.nextSibling) node.nextSibling.nodeValue = afternodecontent;
            // hide the content
            $(this).parent().nextAll().slideToggle("slow");
            // hide the read more button
            $(this).hide("slow");

            // add the readless button
            $(this).parent().nextAll().last().append('<p id="readless-' + index + '" class="readless-wrapper"><a href="#" class="readless">Read Less...</a></p>');    

            // hvea the readless button click work
            $('.readless').click(function() {
                // show the readmore info based off of the index of the button
                $('#readmore-' + index).parent().nextAll().slideToggle("slow");

                // show the read more
                $('#readmore-' + index).show("slow");

                // append the ... and hide the rest of the content
                if (node && node.nextSibling) {
                    // if not at the end of a paragraph remove the rest of the paragraph and append ...
                    var afternodecontent = node.nextSibling.nodeValue;
                    node.nextSibling.nodeValue = " ...";
                }

                // remove the readless button
                $('.readless-wrapper').remove();

                // stop the href
                return (false);
            });
            // stop the href from working
            return (false);

        }); // Close Click function
    }); // Close Each Function
}); // Close document ready
4

0 回答 0