简体   繁体   中英

Jquery (“.selector li:last-child”) vs (“.selector > li:last-child”)

Why is (“.mylist li:last-child”) not working, but (“.mylist > li:last-child”) give the right result and what exactly is the difference?

I àm wondering because my script was working for some time...

<ul class="mylist ">
    <li id="1">a</li>
    <li id="2">b</li>
    <li id="3">c</li>
    <li id="4">d</li>
</ul>

var $LastpostID = $('.mylist li:last').attr('id')
console.log ($LastpostID)
result undefined


var $LastpostID = $('.mylist > li:last').attr('id')
console.log ($LastpostID)
result 4

> means "direct child of": only nodes that are direct children are selected.

While a space means "any descendant of": direct children and children of those children could be selected.

but in your case both should work as same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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