javascript Swap two nodes调换两个元素的位置

Swap two nodes

The function below swaps two given nodes, nodeA and nodeB:

const swap = function (nodeA, nodeB) {
    const parentA = nodeA.parentNode;
    const siblingA = nodeA.nextSibling === nodeB ? nodeA : nodeA.nextSibling;

    // Move `nodeA` to before the `nodeB`
    nodeB.parentNode.insertBefore(nodeA, nodeB);

    // Move `nodeB` to before the sibling of `nodeA`
    parentA.insertBefore(nodeB, siblingA);
};

  

posted @ 2022-11-04 01:09  uuxyz  阅读(33)  评论(0)    收藏  举报
联系我