using javascript's .insertBefore to insert item as last child

前端 未结 6 1005
梦谈多话
梦谈多话 2020-12-18 01:15

I so miss jQuery. I\'m working on a project where I need to get my hands dirty with good \'ol plain Javascript again.

I have this scenario:

parent
           


        
6条回答
  •  太阳男子
    2020-12-18 01:53

    The functionality of insertBefore(newNode, referenceNode) is described as:

    Inserts the specified node before a reference node as a child of the current node. If referenceNode is null, then newNode is inserted at the end of the list of child nodes.

    And since myNodes[i+1] is outside of the array bounds, it returns undefined, which is treated as null in this case. This means you get your desired behavior.

    Edit: Link to the W3 specification of insertBefore()

提交回复
热议问题