How to detect HTMLCollection/NodeList in JavaScript?

前端 未结 7 536
刺人心
刺人心 2020-12-05 02:55

I\'m not sure my current implementation is available all the time:

function isNodeList(nodes) {
    var result = Object.prototype.toString.call(nodes);
    /         


        
7条回答
  •  遥遥无期
    2020-12-05 03:17

    The following should return true, if nodes is of type NodeList

    NodeList.prototype.isPrototypeOf(nodes)
    

    @DavidSpector, for HTMLCollection you can similarly use :

    HTMLCollection.prototype.isPrototypeOf(collection)
    

提交回复
热议问题