了解最后一个孩子

发布于 2024-12-03 01:41:31 字数 1048 浏览 0 评论 0原文

我有以下 html 结构:

<div class="decorator">
    <div class="EC_MyICHP_Item">
        <div class="text">
            <h3><a target="_blank" title="" href="#"></a></h3>
            text here text here text here text here text here text here 
        </div>
    </div>

    <div class="EC_MyICHP_Item">
        <div class="text">
            <h3><a target="_blank" title="" href="#"></a></h3>
            text here text here text here text here text here text here 
        </div>
    </div>

    <div class="EC_MyICHP_Item">
        <div class="text">
            <h3><a target="_blank" title="" href="#"></a></h3>
            text here text here text here text here text here text here 
        </div>
    </div>

    <div class="readmore"><a></a></div>
</div>

我试图通过使用最后一个子项来选择最后一个 EC_MyICHP_Item,但徒劳。 (CSS 和 jQuery)你能帮我吗?

谢谢。

I have the following html structure:

<div class="decorator">
    <div class="EC_MyICHP_Item">
        <div class="text">
            <h3><a target="_blank" title="" href="#"></a></h3>
            text here text here text here text here text here text here 
        </div>
    </div>

    <div class="EC_MyICHP_Item">
        <div class="text">
            <h3><a target="_blank" title="" href="#"></a></h3>
            text here text here text here text here text here text here 
        </div>
    </div>

    <div class="EC_MyICHP_Item">
        <div class="text">
            <h3><a target="_blank" title="" href="#"></a></h3>
            text here text here text here text here text here text here 
        </div>
    </div>

    <div class="readmore"><a></a></div>
</div>

I am trying to select the LAST EC_MyICHP_Item, by using last-child, but in vain. (both CSS and jQuery) Could you help me?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

您需要在选择器的末尾使用 :last-child

div.EC_MyICHP_Item:last-child

http://reference.sitepoint.com /css/pseudoclass-lastchild

示例http://jsfiddle.net/jasongennaro/zrufd/

请注意:这在早期版本的 IE 中不起作用。

编辑

根据关于添加最后一个div的评论,它会造成干扰。你说得对。它确实会导致 :last-child 窒息……至少在我测试过的 Chrome 中是这样。

如果您的 HTML 结构保持不变,即始终为三个 div.EC_MyICHP_Item,您可以执行以下操作

.EC_MyICHP_Item:nth-child(3)

更新示例: http://jsfiddle.net/jasongennaro/zrufd/1/

编辑#2

不幸的是,EC_MyICHP_Item div 的数量各不相同

在这种情况下,我将使用 jQuery:

$('.EC_MyICHP_Item:last')

进一步更新的示例: http://jsfiddle.net/zrufd/2/

You need to use :last-child at the end of the selector.

div.EC_MyICHP_Item:last-child

http://reference.sitepoint.com/css/pseudoclass-lastchild

Example: http://jsfiddle.net/jasongennaro/zrufd/

Please note: this will not work in earlier versions of IE.

EDIT

As per the comment about the last div being added and it interfering. You're right. It does cause :last-child to choke... at least in Chrome where I tested it.

If your HTML structure remains the same, that is, always three div.EC_MyICHP_Item, you could do this

.EC_MyICHP_Item:nth-child(3)

Updated Example: http://jsfiddle.net/jasongennaro/zrufd/1/

EDIT #2

unfortunately the number of EC_MyICHP_Item div's varies

In that case, I would use jQuery:

$('.EC_MyICHP_Item:last')

Further updated example: http://jsfiddle.net/zrufd/2/

烂人 2024-12-10 01:41:31

.EC_MyICHP_Item:last-child 应该可以正常工作。

重要的是要认识到 E:last-child 的意思是“E 元素,它是最后一个子元素”,而不是“E 元素的最后一个子元素”。

.EC_MyICHP_Item:last-child should work well.

It's important to realize that E:last-child means "an E element, which is a last child", not "last child of E element".

浅唱々樱花落 2024-12-10 01:41:31

我添加 ID 只是为了测试目的。

但使用 :last

http://jsfiddle.net/aDbcW/1/

I added the ID for merely testing purposes.

But use :last.

http://jsfiddle.net/aDbcW/1/

拧巴小姐 2024-12-10 01:41:31

.EC_MyICHP_Item:last-child 仅当 div 是其父 div 的最后一个子级时才有效。由于您将 div.readmore 作为父级的最后一个子级,因此您的选择器将不起作用。您需要使用 .EC_MyICHP_Item:last-of-type 代替。

编辑:在 jQuery 中,这将转换为 .EC_MyICHP_Item:last

.EC_MyICHP_Item:last-child would only work if the div is the last child of its parent div. Since you have div.readmore as the last child of the parent, your selector wouldn't work. You need to use .EC_MyICHP_Item:last-of-type instead.

Edit: in jQuery this would translate to .EC_MyICHP_Item:last.

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