jquery遍历表,只有2x TD才能执行某些操作

时间:2012-07-23 21:51:41

标签: jquery expand

我在这里遇到一个特殊的“问题”。 Ti可以满足客户的要求,我需要执行以下操作。遍历页面上的所有表格。对于每个表格,检查它是否恰好有2个TD。如果确实如此,那么我需要进行“阅读更多”实施。

我在另一个页面上使用过这个,我知道我想要折叠和展开的确切div。

$(".more-block").each(function(){
    if ($(this).height() > adjustheight){
        id = this.id;
        id = id.split("_");
        $("#expand"+id[1]).toggle(function() {
                id = this.id;
                $(".more-block"+id).css('height', 'auto').css('overflow', 'visible');
                $('html, body').animate({scrollTop: $(document).height()}, 'slow');
                $("#"+id).attr('src',path+'/images/expand2.png');
                $(".frontpage_box").css('height', '');
            }, function() {
                id = this.id;
                $("#"+id).attr('src',path+'/images/expand.png');
                $(".more-block"+id).animate({height: adjustheight}, "slow", function(){
                    $(".more-block"+id).css('height', adjustheight).css('overflow', 'hidden');
                });         
        });
    };
});

$(".more-block").css('height', adjustheight).css('overflow', 'hidden');

有什么想法吗? 当我浏览TD时,我可以添加“more-less”和“more-lessexpand1”类(数字是增量)。这可能吗?我不知道如何运行表格,只有在找到两个TD时才执行此操作,并且只在第二个TD上执行此操作。

哦顺便说一句......重要的是它只有第二个TD崩溃和扩展。左边的是不允许的。如果有人能帮助我找到感兴趣的TD,我或许可以解决这个问题。

请帮忙

2 个答案:

答案 0 :(得分:1)

$('table').filter(function(
    return $('td', this).length==2;
)).find('td').last().addClass('more-less more-lessexpand1');

答案 1 :(得分:0)

  

“如果有人能帮助我找到感兴趣的TD,我或许可以解决这个问题。”

这样的东西会找到感兴趣的表,假设你没有嵌套表:

$("table").each(function() {
   var $tds = $(this).find("td");
   if ($tds.length != 2)
       return;
   // this is a table with exactly two cells, so do something
   // $tds.eq(0) will reference first td;
   // $tds.eq(1) will reference second td;
   // So to add the classes to the second cell...
   $tds.eq(1).addClass("more-less more-lessexpand1");
});
相关问题