隐藏没有唯一标识符的选定元素

发布于 2024-11-26 00:03:35 字数 609 浏览 5 评论 0原文

使用下面的代码示例,我想做的是将图像保留在前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

为你鎻心 2024-12-03 00:03:35

使用 jQuery 的 gt() 选择器

要隐藏与这些图像关联的整个 li,请使用:

$("#list li:gt(1)").hide ();

要仅隐藏图像,请使用:

$("#list li:gt(1) img").hide ();

在 jsFiddle 上查看它的实际应用。

Use jQuery's gt() selector.

To hide the entire li associated with those images use:

$("#list li:gt(1)").hide ();

To hide just the images use:

$("#list li:gt(1) img").hide ();

See it in action at jsFiddle.

一场春暖 2024-12-03 00:03:35

查看 :nth-child() JQuery 选择器。

$('#list li img:nth-child(4)').hide();
$('#list li img:nth-child(3)').hide();
// indexing from 1

或者:

var imgs = $("#list li img");
imgs.eq(2).hide();
imgs.eq(3).hide();
// indexing from 0

Look at the :nth-child() JQuery selector.

$('#list li img:nth-child(4)').hide();
$('#list li img:nth-child(3)').hide();
// indexing from 1

or:

var imgs = $("#list li img");
imgs.eq(2).hide();
imgs.eq(3).hide();
// indexing from 0
心头的小情儿 2024-12-03 00:03:35

这不是一种非常通用的形式,但可以在您的情况下完成工作:

$("#list li img").eq(2).hide(); // hides the 3rd img
$("#list li img").eq(3).hide(); // hides the 4th img

一种更抽象的方式,这将隐藏第二个之后的所有 img:

$("#list li img").each(function(i) {
    if (i > 1) {
        $(this).hide();
    }
});

This isn't a very universal form, but does the job in your situation:

$("#list li img").eq(2).hide(); // hides the 3rd img
$("#list li img").eq(3).hide(); // hides the 4th img

A more abstract way, this would hide all img after the 2nd:

$("#list li img").each(function(i) {
    if (i > 1) {
        $(this).hide();
    }
});
旧人 2024-12-03 00:03:35

用CSS来做,很简单

ul#list li img:nt-child(3) , ul#list li img:nt-child(4){
    display:none;
}

Do it with CSS, very simple

ul#list li img:nt-child(3) , ul#list li img:nt-child(4){
    display:none;
}
笑忘罢 2024-12-03 00:03:35

或者最简单的方法:

$('#list li:eq(2) img').hide();

DEMO

Or the easiest way:

$('#list li:eq(2) img').hide();

DEMO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文