Selecting only first-level elements in jquery

前端 未结 10 2197
忘了有多久
忘了有多久 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:59

    I had some trouble with nested classes from any depth so I figured this out. It will select only the first level it encounters of a containing Jquery Object:

    var $elementsAll = $("#container").find(".fooClass");4
    
    var $levelOneElements = $elementsAll.not($elementsAll.children().find($elementsAll));
    
    $levelOneElements.css({"color":"red"})
    
    
    Container
    Level One
    Level Two
    Level One
    Level Two

提交回复
热议问题