How can I add a child to a node at a specific position?

前端 未结 3 1815
别跟我提以往
别跟我提以往 2021-02-19 02:26

I have a node which has two children: an HTML text and an HTML element.

Installation on server

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-19 03:02

    You can use NodeSet#before like this:

    node.children.before(span_node)
    

    NodeSet#before is a short-cut method to insert an item as the first element. A more general-purpose solution is to use Node#before and Node#after. For example, some_node.before foo will add the node foo as the sibling directly before some_node. Node#after is similar. Note that this:

    node.children.first.before(span_node)
    

    is thus equivalent to the solution above.

提交回复
热议问题