打开以前隐藏的部分时隐藏按钮

时间:2014-03-28 13:02:31

标签: jquery

我有一个文本页面,第一段是可见的,其余段落是隐藏的,可以通过点击“阅读更多”链接使其可见......但是“阅读更多”按钮仍然可见其余的段落都是开放的。我想在点击时隐藏“阅读更多”链接并再次显示当用户关闭容器时,如果/当用户关闭之前隐藏的段落下面是我现在工作的时候,这可能真的很容易但我是一个jQuery noob。

由于

$(function()
{
    $(".about_text_wrap .readMore_aboutText")
        .next()
        .hide()
        .append('<input type="button" value="close" />');

    $(".about_text_wrap .readMore_aboutText").click(function()
    {

        var $this = $(this);
        $this.parent().find('.more_about_textWrap').slideUp("fast");
        $this.next().slideDown("slow");

    });

    $("input").click(function()
    {

        $(this).parent().slideUp();

    });
});

HTML

<div class="about_text_wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p><span class="readMore_aboutText">Read More</span>
    <div class="more_about_textWrap">
        <p>Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. </p>
        <p>Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. </p>
        <p>Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. </p>
    </div><!-- about_text_moreWrap -->
</div><!-- about_text_wrap-->

3 个答案:

答案 0 :(得分:1)

您可以使用.hide()在点击时隐藏read more链接,并.show()在点击关闭按钮时将其显示回来:

$(function () {

    $(".about_text_wrap .readMore_aboutText").next().hide().append('<input type="button" value="close" />');

    $(".about_text_wrap .readMore_aboutText").click(function () {
        var $this = $(this);
        $this.hide();
        $this.parent().find('.more_about_textWrap').slideUp("fast");
        $this.next().slideDown("slow");
    });

    $("input").click(function () {
        $(this).parent().slideUp(function() {
            $(".about_text_wrap .readMore_aboutText").show();
        });
    });
});

<强> Fiddle Demo

答案 1 :(得分:0)

隐藏后你附加哪个不起作用。使用end()然后追加哪个确保以正确的结构附加div:

$(".about_text_wrap .readMore_aboutText").next().hide().end().append('<input type="button" value="close" />');

答案 2 :(得分:0)

另一种可能性是简化你的jQuery内容,但是你需要调整你的HTML,我不知道这是否是一个选项:

的jQuery / JS:

$(".readMore_aboutText").click(function() {
    $(".more_about_textWrap").slideToggle();
    $(".readMore_aboutText").toggle();
});

CSS:

.more_about_textWrap { display: none; }
.hideMe { display: none; }

HTML:

<span class="readMore_aboutText">Read More</span>
<span class="readMore_aboutText hideMe">Close</span>

请参阅此Fiddle demo