How to find the first direct child element of a div

后端 未结 4 909
醉酒成梦
醉酒成梦 2021-02-02 11:56

This should be easy but I am not able to do it.

I have a div element with id \"LeftScrollableDiv\" and I am trying to find the first child element under it:



        
4条回答
  •  天命终不由人
    2021-02-02 12:20

    The :first-child checks if its subject is the first child of its parent. In thise case you are trying to select element with id #LeftScrollableDiv that is the first child of its parent. All the : selectors just filter previous selection, they don't select any new elements.

    There is unfortunately no .firstChild method in jQuery so you must always select all the children first and then take out the first one.

    A possible selector to do it is:

    "#LeftScrollableDiv > :first"
    

    Which is far more efficient than > :first-child.

提交回复
热议问题