jQuery index() in vanilla javascript

前端 未结 5 1645
北荒
北荒 2020-12-01 12:16

As per the jQuery api, the complementary operation to .get(), which accepts an index and returns a DOM node, .index() can take a DOM node and returns an index.

5条回答
  •  被撕碎了的回忆
    2020-12-01 12:46

    You can build your own function :

    function indexInParent(node) {
        var children = node.parentNode.childNodes;
        var num = 0;
        for (var i=0; i

    Demonstration (open the console)

提交回复
热议问题