首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery显示/隐藏以在表格之间循环

jQuery显示/隐藏以在表格之间循环
EN

Stack Overflow用户
提问于 2010-02-25 20:50:47
回答 4查看 882关注 0票数 2

我需要将jquery设置为遍历表格,以确保它显示/隐藏正确的表格。

基本的HTML结构是:

代码语言:javascript
复制
<p><a href="#" class="month-next">Next Month</a></p>

<table class="month-show"><tr><td>blurb</td></tr></table>
<table class="month-hide"><tr><td>blurb</td></tr></table>
<table class="month-hide"><tr><td>blurb</td></tr></table>
<table class="month-hide"><tr><td>blurb</td></tr></table>

因此,到目前为止,在jQuery中我有:

代码语言:javascript
复制
$(".month-next").click(function () {
 $("table.month-show").hide();
 HERE I NEED SOME CODE TO ONLY SHOW THE NEXT TABLE IN THE HTML .show();
 return false;
});
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-02-25 20:57:02

代码语言:javascript
复制
$(".month-next").click(function () {
 $("table.month-show").hide().next().show();
 return false;
});

您可能还想更改类,以便下一次调用也能正常工作(edit:建议的插入更改):

代码语言:javascript
复制
$(".month-next").click(function () {
    $("table.month-show")
        .hide()
        .removeClass("month-show")
        .addClass("month-hide")
        .next()
        .show()
        .removeClass("month-hide")
        .addClass("month-show");
    if ($("table").hasClass('month-show').length < 1) {
        $('table').eq(0).addClass('month-show');
    }
 return false;
});
票数 4
EN

Stack Overflow用户

发布于 2010-02-25 23:00:42

这是我如何使用它的一个工作示例:http://jsbin.com/ogika

CSS:

代码语言:javascript
复制
.month.hide { display: none; }

HTML

代码语言:javascript
复制
<p><a href="#" class="month-next">Next Month</a></p>

<table class="month"><tr><td>a</td></tr></table>
<table class="month hide"><tr><td>b</td></tr></table>
<table class="month hide"><tr><td>c</td></tr></table>
<table class="month hide"><tr><td>d</td></tr></table>

JavaScript:

代码语言:javascript
复制
$(function () {
    $(".month-next").click(function () {
        var months = $(".month"), numMonths = months.length, curr = 0;
        return function () {
            //Hide the current table, by adding the 'hide' class
            months.eq(curr).addClass("hide"); 

            //Get the index of next table in the list
            curr = (curr + 1) % numMonths;  

            //Show the next table, by removing the 'hide' class
            months.eq(curr).removeClass("hide"); 
        }
    }());
});

在上面的代码中,months是本例中tables...which的列表,包含4个元素;numMonths是elements...ie的数量,4。

上面代码的“魔力”是这一行:curr = (curr + 1) % numMonths;

该行获取要显示的下一个元素的索引,它以循环方式工作。

让我们举个例子:

代码语言:javascript
复制
   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================

现在,假设curr为0:

代码语言:javascript
复制
   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
   ^

//curr is currently 0
curr = (curr + 1) % numMonths; //(0 + 1) % 4
//now, curr is 1

   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
         ^

执行该行代码后,curr变为1:(0 + 1) %4= 1。这意味着要显示的元素的下一个索引是1。因此,我们通过删除hidemonths.eq(curr).removeClass("hide");来获取下一个元素并显示它

现在让我们看一下示例,其中curr是最后一个元素:3

代码语言:javascript
复制
   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
                     ^

//curr is currently 3
curr = (curr + 1) % numMonths; //(3 + 1) % 4
//now, curr is 0

   0     1     2     3
=========================
|  a  |  b  |  c  |  d  |
=========================
   ^

正如您所看到的,curr现在是0...and,因此循环继续。

票数 2
EN

Stack Overflow用户

发布于 2010-02-25 21:05:32

这就是你要的。这应该是可行的(甚至可以处理循环)。由于页面上的其他标记,您可能需要进行一些细微的调整。

代码语言:javascript
复制
<html>
<head>
<script language="javascript" src="jquery-1.3.2.js"></script>

<script language="javascript">
$(document).ready(function(){

    $(".month-next").click(function () {
     var $monthShow = $("table.month-show");
     var $monthNext = $monthShow.next('table.month-hide');
    if($monthNext.length == 0){
        $monthNext = $('table.month-hide:first');
        }
     $monthShow.removeClass('month-show').addClass('month-hide');
     $monthNext.removeClass('month-hide').addClass('month-show');
     return false;
    });

});
</script>
<style type="text/css">
.month-show{ display:block;}
.month-hide{ display:none;}

</style>
</head>
<body>

<p><a href="#" class="month-next">Next Month</a></p>

<table class="month-show"><tr><td>blurb1</td></tr></table>
<table class="month-hide"><tr><td>blurb2</td></tr></table>
<table class="month-hide"><tr><td>blurb3</td></tr></table>
<table class="month-hide"><tr><td>blurb4</td></tr></table>

</body>
</html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2334041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档