了解最后一个孩子
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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
在这种情况下,我将使用 jQuery:
进一步更新的示例: 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
In that case, I would use jQuery:
Further updated example: http://jsfiddle.net/zrufd/2/
.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".我添加 ID 只是为了测试目的。
但使用
:last
。http://jsfiddle.net/aDbcW/1/
I added the ID for merely testing purposes.
But use
:last
.http://jsfiddle.net/aDbcW/1/
.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 havediv.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
.