简体   繁体   English

jquery:最后一个孩子没能正常工作

[英]jquery :last-child did not work properly

jQuery $("div span[z*='w']:last-child").css({color: "red"}); jQuery $("div span[z*='w']:last-child").css({color: "red"});

and the html 和HTML

<div>
   <span z="ww">Sam</span>  <!-- Didn't works -->
   <span>Sam 2</span>
</div>
<div>
    <span z="ww">Glen,</span>
    <span z="ww">David</span> <!-- Works -->
</div>

How to select the span from the first div? 如何从第一个div中选择范围?

Live example http://jsbin.com/izubaj/edit#javascript,html,live 实例http://jsbin.com/izubaj/edit#javascript,html,live

Your selector actually chooses the span elements and then applies the last child pseudo selector to that collection, try the following: 您的选择器实际上选择了span元素,然后将最后一个子伪选择器应用于该集合,请尝试以下操作:

$("div").find('span[z*="w"]:last').css({color: "red"});

See here for update. 请看这里更新。

尝试这个:

$("span[z*='w']:last", 'div').css({color: "red"});

I may be misunderstanding the question, but I think you want the span that has "Sam 2" as well as "David". 我可能误解了这个问题,但我认为你想要的是“Sam 2”以及“David”的span

<div>
    <span z="ww">Sam</span>  <!-- Didn't works -->
    <span>Sam 2</span>
</div>

If that's the case, try: 如果是这种情况,请尝试:

$('div span[z*="w"]').siblings(':last-child').css({color: 'red'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM