SlideToggle多次点击多次阅读,只显示一次,隐藏其他人。

时间:2014-02-05 12:08:31

标签: javascript jquery show-hide slidetoggle multiple-choice

如果我在一个特定read more上点击多次,slideToggle函数每次多读/少读时都会更改我的代码,该如何更改我的代码?显示/隐藏内容,其他人保持read more甚至,当我点击read more的其他链接时,旧的 slideUp 以及 slideDown 的新用slideToggle函数。

我的HTML

<!-- === h1 === -->
    <h1>Lorem ipsum dolor sit amet</h1>
        <p>Ut enim ad minim veniam, quis nostrud exercitation.</p>
        <p class="more">Duis aute irure dolor in reprehenderit in voluptate.</p>
        <a class="read_more">... Read More</a>
    <!-- === h2 === -->             
    <h2>Consectetur adipisicing elit</h2>
        <p>On the other hand, we denounce with righteous.</p>
        <p class="more">Gemoralized by the charms of pleasure of the moment.</p>
        <a class="read_more">... Read More</a> 
    <!-- === h3 === -->                  
    <h3>Sed do eiusmod tempor incididunt</h3>
        <p>The wise man therefore always holds.</p>
        <p class="more">Et harum quidem rerum facilis est et expedita distinctio.</p>
        <a class="read_more">... Read More</a>

我的Jquery

var rm = $(".read_more"),
    moreText = "... Read More",
    lessText = "... Read Less";

rm.click(function () {
  rm.text(moreText);

var $this = $(this);
var more = $this.text($this.text() == moreText ? lessText : moreText).prev(".more").slideToggle("normal");

$('.more').not(more).slideUp();
});

1 个答案:

答案 0 :(得分:0)

您的代码几乎是正确的。刚刚对这个小改动进行了功能并添加了一点CSS。

    var rm = $(".read_more"),
    moreText = "... Read More",
    lessText = "... Read Less";

rm.click(function () {
    var $this = $(this);
    $this.prev().slideToggle();
    $this.text($this.text() == moreText ? lessText : moreText);
});

添加以下CSS。

p.more {
    display: none;
}

检查以下小提琴,http://jsfiddle.net/7ZyuP/2/