Selecting only first-level elements in jquery

前端 未结 10 2212
忘了有多久
忘了有多久 2020-11-28 03:14

How can I select the link elements of only the parent

    from a list like this?

    • Link
10条回答
  •  醉话见心
    2020-11-28 03:58

    $("ul > li a")
    

    But you would need to set a class on the root ul if you specifically want to target the outermost ul:

      ...

    Then it's:

    $("ul.rootlist > li a")....
    

    Another way of making sure you only have the root li elements:

    $("ul > li a").not("ul li ul a")
    

    It looks kludgy, but it should do the trick

提交回复
热议问题