How to detect HTMLCollection/NodeList in JavaScript?

前端 未结 7 555
刺人心
刺人心 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:32

    Here is how to test if an object is a NodeList in modern browsers:

    if (nodes instanceof NodeList) {
      // It's a NodeList object
    }
    

提交回复
热议问题