开发者

not able to modify last <li> with jquery?

开发者 https://www.devze.com 2023-04-13 02:40 出处:网络
I have some nested <UL><li><ul><li>text</li></ul></li></UL>, I want to modify last child using jquery, but when I am writing $(\"#id li:last-child\") it

I have some nested <UL><li><ul><li>text</li></ul></li></UL>, I want to modify last child using jquery, but when I am writing $("#id li:last-child") it affecting all nested <li>s. here is the code.

<ul id="ddmenu">
<li><a href="#">Home></a>
    <ul>        

          <li><a href="#">QA Managers Dashboard</a></li>    
    </ul> 
 </li>

<li><a href="#">Req Mgmt</a>
    <ul>
        <li><a href="#">Panel Beating</a></li>
        <li><a href="#">Car Inspection</a></li>
    </ul>

</li>
<li>&开发者_开发问答lt;a href="#">Test</a>
    <ul>
        <li><a href="#">Work Place</a></li>
        <li><a href="#">Special Events</a></li>
    </ul>
</li>
<li><a href="#">Project</a>
    <ul>
        <li><a href="#">Enquiry</a></li>        
    </ul>
</li>
   <li><a href="#">More</a></li>

I want to modify the <li> which contains text "More"... how to do that?? any help please?


Use > to get only direct descendants like this

$("#id > li:last-child")


I you know how much items you have, you can try $("#id li:nth-child("+nbrItems+")");

Hope this helps


Try using the below code,

$("#id > li:last-child") 

the symbol ">" represents direct children of the parent element.


Just as out of general interest (and I accept that your problem's already solved) but the following also works:

$('li:last-child:last').css('color','red');

JS Fiddle demo.

I understand why it works this way, but I must confess that when I first wrote that selector I expected that $('li:last-child:first') would work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号