lastChild not working

前端 未结 6 866
予麋鹿
予麋鹿 2021-01-25 01:20

I have the following code:







        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 01:49

    Your elements probably have text nodes around them, so the last child node of the outer

    won't necessarily have an "id" attribute.

    I'm not sure if all browsers support it, but there's a "lastElementChild" property that explicitly gets only elements, and not things like comment nodes or text nodes. Failing that, you could just loop through the node list looking for type 1 nodes.

提交回复
热议问题