隐藏没有唯一标识符的选定元素
使用下面的代码示例,我想做的是将图像保留在前 2 个列表对象中,但隐藏对象 3 和 4 中的图像。
从第四个对象中删除图像非常简单,但是我如何删除来自第三个物体的图像,同时保留第二个物体的图像。
<ul id="list">
<li class="start"><img class="postImage" src="image1.png" /><p>Some text here</p></li>
<li><img class="postImage" src="image2.png" /><p>Some text here</p></li>
<li><img class="postImage" src="image3.png" /><p>Some text here</p></li>
<li class="end"><img class="postImage" src="image4.png" /><p>Some text here</p></li>
</ul>
Using my code example below, what I would like to do is keep the images in the first 2 list objects, but hide the images in objects 3 and 4.
Removing the image from the 4th object is simple enough, but how can i remove the image from the 3rd object while keeping the image on the 2nd object.
<ul id="list">
<li class="start"><img class="postImage" src="image1.png" /><p>Some text here</p></li>
<li><img class="postImage" src="image2.png" /><p>Some text here</p></li>
<li><img class="postImage" src="image3.png" /><p>Some text here</p></li>
<li class="end"><img class="postImage" src="image4.png" /><p>Some text here</p></li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 jQuery 的
gt()
选择器。要隐藏与这些图像关联的整个
li
,请使用:要仅隐藏图像,请使用:
在 jsFiddle 上查看它的实际应用。
Use jQuery's
gt()
selector.To hide the entire
li
associated with those images use:To hide just the images use:
See it in action at jsFiddle.
查看 :nth-child() JQuery 选择器。
或者:
Look at the :nth-child() JQuery selector.
or:
这不是一种非常通用的形式,但可以在您的情况下完成工作:
一种更抽象的方式,这将隐藏第二个之后的所有 img:
This isn't a very universal form, but does the job in your situation:
A more abstract way, this would hide all img after the 2nd:
用CSS来做,很简单
Do it with CSS, very simple
或者最简单的方法:
DEMO
Or the easiest way:
DEMO