jquery:gt问题..试图显示更多

时间:2012-02-12 05:07:24

标签: jquery

NI正试图在网站上设置一个新闻区域......每个项目都包含在自己的div类中,名为.newsitem。我希望能够点击.showmorenews来显示其他项目。我究竟做错了什么?我无法理解,我对jquery相当新。谢谢!

$(document).ready(function(){
    var remixnewsshow = 2;
    remixnewsshow--; //fix variable since the index for :gt starts at 0
    $(".newsitem:gt(" + remixnewsshow + ")").hide();
    $(".showmorenews").click(function () {
        remixnewsshow++;
        $(".newsitem:gt(" + remixnewsshow + ")").show(); 
    });
});

HTML:

                <div class="box-news">
                    <h3>Recent News</h3>
                    <div class="newsitem">
                        <span class="month">Jan</span>
                        <span class="day">16</span>
                        <span class="news">Text text text</span>
                    </div>
                    <div class="newsitem">
                        <span class="month">Jan</span>
                        <span class="day">2</span>
                        <span class="news">Text text text</span>
                    </div>
                    <div class="newsitem">
                        <span class="month">Oct</span>
                        <span class="day">3</span>
                        <span class="news">Text Text Text</span>
                    </div>
                    <div class="showmorenews"><a href="#blank">View Another News Item</a></div>
                </div>

1 个答案:

答案 0 :(得分:0)

我认为您所寻找的实际上是将第二个gt更改为eq

 $(".showmorenews").click(function () {
        remixnewsshow++;
        $(".newsitem:eq(" + remixnewsshow + ")").show(); 
    });

你所做的事情的问题是你最初显示的是2个元素,然后点击时你会显示大于2的所有元素,其余都是。您只想显示下一个元素。