Javascript TypeError ____ is not a function

后端 未结 4 1617
野性不改
野性不改 2021-01-27 02:46

I am currently using the Chrome console to do some debugging for a Greasemonkey script.

From the console I run var opp = document.querySelectorAll(\'a[class=\"F-re

4条回答
  •  生来不讨喜
    2021-01-27 03:10

    querySelectorAll returns a NodeList. This is similar to an array (it has a .length property and you can index it with []), but it's not actually an array, and doesn't have most of the array methods. If you want to use array methods on an array-like object, you have to call the method explicitly:

    Array.prototype.splice.call(opp, 0, 1);
    

    or:

    [].splice.call(opp, 0, 1);
    

    However, another difference between arrays and NodeLists is that you can't modify NodeList in place, which .splice tries to do; you can only read them like arrays. You should just use .slice() to extract the parts you want. Or convert the NodeList to an array first, and then operate on that. See

    Fastest way to convert JavaScript NodeList to Array?

提交回复
热议问题