2014-12-11 89 views
1

试图掩盖过去的TD,如果有3层以上的TD的一排。东西不起作用隐藏最后一个孩子,如果有3个以上的孩子

这里是jQuery代码

 if (screen.width < 320) { 
     $(".slider-navigation-thumbs table tr").each(function(){ 
      var tdsnumber = $(this).children().length; 
      if (tdsnumber > 3) { 
      $(this).child().last().hide(); 
      } 
     }); 
     } 

其他信息 对于实验的缘故,去掉了屏幕尺寸的阅读部分。没有它就能正常工作。所以,我猜错误是在那里,但无法弄清楚什么是错的。试图增加320至340,但仍然没有去。

+0

等待,是你与“隐藏最后一个孩子”或者某个周围的状况的问题?顺便说一句,除非你做了一件神奇的事情'jQuery.fn.child'不存在,所以它不应该“工作正常”。 – 2014-12-11 04:52:47

回答

0

您需要.find('td').children()更换.child()

if (screen.width < 320) { 
    $(".slider-navigation-thumbs table tr").each(function(){ 
     var tdsnumber = $(this).children().length; 
     if (tdsnumber > 3) { 
     $(this).find('td').last().hide(); 
     } 
    }); 
    } 
+0

这一个不太工作 – whatzi77 2014-12-11 04:00:29

+0

检查控制台中的错误? – 2014-12-11 04:02:45

+0

增加了一些额外的信息 – whatzi77 2014-12-11 04:43:44

2

没有child()方法。你可能想要children()

0

没有.child()方法,

if (screen.width < 320) { 
    $(".slider-navigation-thumbs table tr").filter(function(){ 
     return $(this).children().length > 3 
    }).children('td:last-child').hide() 
} 
+0

不工作... – whatzi77 2014-12-11 04:00:03

+0

@ whatzi77可以共享HTML样品 – 2014-12-11 04:09:56

+0

增加了一些额外的信息 – whatzi77 2014-12-11 04:43:05

1

您使用.children().length,但后来你使用.child();这显然是错误的。

这就是说,这里的东西比较简单:

$(".slider-navigation-thumbs table tr > td:gt(2):last").hide(); 

它选择孩子里面<tr>具有比2更高的指数(起这意味着第四个孩子),然后选择该组的最后一个元素;最后,它隐藏了结果元素。

如果只有三个子元素,它会返回一个空集,因此没有东西隐藏。