Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果有3个以上的子项,则隐藏最后一个子项_Javascript_Jquery - Fatal编程技术网

Javascript 如果有3个以上的子项,则隐藏最后一个子项

Javascript 如果有3个以上的子项,则隐藏最后一个子项,javascript,jquery,Javascript,Jquery,如果连续有超过3个td,则尝试隐藏最后一个td。有些东西坏了 这是jquery代码 if (screen.width < 320) { $(".slider-navigation-thumbs table tr").each(function(){ var tdsnumber = $(this).children().length; if (tdsnumber > 3) { $(this).

如果连续有超过3个td,则尝试隐藏最后一个td。有些东西坏了

这是jquery代码

      if (screen.width < 320) {
        $(".slider-navigation-thumbs table tr").each(function(){
           var tdsnumber = $(this).children().length;
          if (tdsnumber > 3) {
            $(this).child().last().hide();
          } 
        });
      }
if(屏幕宽度<320){
$(“.slider navigation thumbs table tr”)。每个(函数(){
var tdsnumber=$(this).children().length;
如果(tdsnumber>3){
$(this.child().last().hide();
} 
});
}
其他信息
为了实验起见,移除了屏幕尺寸读取部分。没有它也行。所以,我猜错误就在某个地方,但我不知道出了什么问题。尝试将320增加到340,但仍然没有成功。

您需要将
.child()
替换为
.find('td')
.children()

if(屏幕宽度<320){
$(“.slider navigation thumbs table tr”)。每个(函数(){
var tdsnumber=$(this).children().length;
如果(tdsnumber>3){
$(this.find('td').last().hide();
} 
});
}
没有
child()
方法。您可能需要
children()

没有
.children()
方法

if (screen.width < 320) {
    $(".slider-navigation-thumbs table tr").filter(function(){
        return  $(this).children().length > 3
    }).children('td:last-child').hide()
}
if(屏幕宽度<320){
$(“.slider导航拇指表tr”).filter(函数(){
返回$(this).children().length>3
}).children('td:last child').hide()
}

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

也就是说,这里有一些更简单的东西:

$(".slider-navigation-thumbs table tr > td:gt(2):last").hide();
它选择
中索引大于2的子元素(表示第四个子元素之后),然后选择该集合的最后一个元素;最后,它隐藏了结果元素


如果只有三个子元素,它将返回一个空集,因此不会隐藏任何内容。

@whatzi77您可以共享html文件吗info@whatzi77您的代码是否在窗口大小调整处理程序中。。。如果不将其移动到
$(window).resize(function(){…})
添加了一些额外的信息我确信
screen.width<320
在这里没有得到满足。尝试警告值
屏幕。宽度
我尝试了2300的宽度,但仍然不起作用等等,你的问题是与“隐藏最后一个孩子”有关还是与它周围的条件有关?顺便说一句,除非你正在做一些神奇的事情,否则jQuery.fn.child不存在,所以它不应该“正常工作”。
$(".slider-navigation-thumbs table tr > td:gt(2):last").hide();