【问题标题】:jQuery toggle function for multiple div's多个 div 的 jQuery 切换功能
【发布时间】:2013-02-12 15:18:18
【问题描述】:

当我点击 div.post-read 并再次点击然后隐藏它时,我有显示 div.post-text 全部内容的功能。 但是有些东西不能正常工作,当我对第一个文本执行此操作时,一切都很好,但是如果我在第一个打开时单击第二个或第三个,那么它不会向后滑动或有时不会打开。 我的功能有什么问题?

$(document).ready(function() {
var read = $(".post-read");
        read.click(function() {
            if (read.html() == 'Read more') {
                $(this).html("Show less");
                $(this).prev().removeClass('post-text').addClass('post-text-open');
                $(this).prev().hide();
                $(this).prev().slideToggle("slow");
            } else {
                $(this).html("Read more");
                $(this).prev().removeClass('post-text-open').addClass('post-text');
                $(this).prev().hide();
                $(this).prev().slideToggle("slow");
            }
            });
        });     

HTML:

<div id="posts">
                <div class="post">
                    <div class="post-img">
                        <img alt="" src="img/post-pic.png">
                    </div>
                    <div class="post-text">text ... ... ...</div>
                    <div class="post-read">Read more</div>
                    <div class="clear"></div>
                </div>
                <div class="post">
                    <div class="post-img">
                        <img alt="" src="img/post-pic.png">
                    </div>
                    <div class="post-text">text ... ... ...</div>
                    <div class="post-read">Read more</div>
                    <div class="clear"></div>
                </div>
                <div class="post">
                    <div class="post-img">
                        <img alt="" src="img/post-pic.png">
                    </div>
                    <div class="post-text">text ... ... ...</div>
                    <div class="post-read">Read more</div>
                    <div class="clear"></div>
                </div>
            </div>

CSS:

.post-text {float: left;width: 457px;padding-left: 16px; overflow:hidden; height:72px;}
.post-text-open {float: left;width: 457px;padding-left: 16px; overflow: auto; height:auto;}

【问题讨论】:

  • $(this).prev().hide(); $(this).prev().slideToggle("slow");所以你把它隐藏起来,在它隐藏后你用滑动再次显示它。?

标签: javascript jquery function toggle


【解决方案1】:

改变这个

if (read.html() == 'Read more') {

到这里

if ($(this).html() == 'Read more') {

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多