“Array.prototype.slice: 'this' is not a JavaScript object” error in IE8

前端 未结 3 1122
名媛妹妹
名媛妹妹 2021-01-12 01:59

It is my understanding that IE8 has access to the Array.prototype.slice method. Yet when I try to call it to turn a NodeList into an array, it give

3条回答
  •  有刺的猬
    2021-01-12 02:45

    Update: A NodeList can be treated as an array in some ways - you don't actually have to do anything special with it before you can loop over it, for example:

    var aDivs = [];
    for (var = i = 0; i < divs.length; i++) {
        aDivs.push(divs[i]);
    }
    

    This will create an array with all of the nodes that matched when you ran document.getElementsByTagName()

    See this question for a full explanation of why slice works with a NodeList in some browsers but not others, but it boils down this this sentence from the specification:

    Whether the slice function can be applied successfully to a host object is implementation-dependent.

提交回复
热议问题