JQuery:隐藏孩子,展示第n个孩子? [英] JQuery: Hide children, show nth child?

查看:98
本文介绍了JQuery:隐藏孩子,展示第n个孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很奇怪,应该很简单。

This is really weird and should be simple.

我在div中的标签内有一系列图像,例如:

I have an array of images within a tags within a div, eg:

<div id="images">
<a href="#"><img src="img1.jpg"/></a>
<a href="#"><img src="img2.jpg"/></a>
<a href="#"><img src="img3.jpg"/></a>
</div>

我想隐藏所有这些,但循环显示第n个,所以我创建了这个图像滑块样式脚本:

I want to hide all of them, but loop through and show the nth one, so I created this image slider style script:

var atags = $('#images').children().length;

$('#images').children().hide();

$('#images a:first').show();

var i=0

while (i <= atags){

$('#images').children().delay(4000).hide();

$("images:nth-child(" + i + ")").show();

i = i + 1;
}

问题在于没有其他标签,尽管第一个出现了循环,得到显示。它们都保持隐藏状态.show()。它出现在行 $(images:nth-​​child(+ i +))。show(); 只是不起作用。

The issue is that no other a tags, despite the first out side the loop, get displayed. They all remain hidden dispite the .show(). It appears the line $("images:nth-child(" + i + ")").show(); just doesnt work.

有人能指出我正确的方向吗?

Can anyone point me in the right direction with this?

推荐答案

images!= #images 加上你需要选择不是容器的实际图像:

images != #images plus you need to select the actual images not the container:

$("#images img:nth-child(" + i + ")").show();

但我只想使用 eq ,而不是如果上面的图像是里面标签,请确定以上内容是否有效:

But I would just use eq, not sure if the above will work given that the images are inside a tag:

$("#images img").eq(i).show();

在任何情况下,您都不需要循环,只需使用jQuery的每个来循环集合。

In any case, you don't need that while loop, just use jQuery's each to loop the collection.

另请注意延迟只有在有动画队列时才有效,而不是你的情况。

Also note that delay only works if there's an animation queue and this not your case.

这篇关于JQuery:隐藏孩子,展示第n个孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆