Android:未捕获的类型错误:对象函数 Array() { [native code] } 没有方法“from”

ATE*_*TES 5 javascript jquery android webview jsfiddle

WebView使用 html 文件和getTopRow()从 html 文件调用javascript 方法的按钮。在jsfiddle 上工作但 android studio 抛出以下错误的方法:

04-03 22:23:39.437 28260-28260/com.site.project E/Web 控制台:未捕获的类型错误:对象函数 Array() { [native code] } 在 file:///android_asset/ 处没有方法“来自” zbe/zbe.html:9

JavaScript 方法:

function getTopRow(){
    var elements = document.querySelectorAll("p");
    var obj = null;
    window.onscroll = function() {
        if (obj != null) {
          obj.style.backgroundColor = "";
        }
        var top = Number.POSITIVE_INFINITY;
        Array.from(elements).forEach(function(o) {
        var t = o.getBoundingClientRect(o).top;
        if (t > 0.0 && top > t) {
          obj = o;
          top = t;
        }
        });
        if (obj != null) {
            obj.style.backgroundColor = "red";
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

ATE*_*TES 2

将 Array.from 导入到 html 文件并解决问题。

\n\n
// Production steps of ECMA-262, Edition 6, 22.1.2.1\n// Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from\nif (!Array.from) {\n  Array.from = (function () {\n    var toStr = Object.prototype.toString;\n    var isCallable = function (fn) {\n      return typeof fn === \'function\' || toStr.call(fn) === \'[object Function]\';\n    };\n    var toInteger = function (value) {\n      var number = Number(value);\n      if (isNaN(number)) { return 0; }\n      if (number === 0 || !isFinite(number)) { return number; }\n      return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));\n    };\n    var maxSafeInteger = Math.pow(2, 53) - 1;\n    var toLength = function (value) {\n      var len = toInteger(value);\n      return Math.min(Math.max(len, 0), maxSafeInteger);\n    };\n    // The length property of the from method is 1.\n    return function from(arrayLike/*, mapFn, thisArg */) {\n      // 1. Let C be the this value.\n      var C = this;\n      // 2. Let items be ToObject(arrayLike).\n      var items = Object(arrayLike);\n      // 3. ReturnIfAbrupt(items).\n      if (arrayLike == null) {\n        throw new TypeError("Array.from requires an array-like object - not null or undefined");\n      }\n      // 4. If mapfn is undefined, then let mapping be false.\n      var mapFn = arguments.length > 1 ? arguments[1] : void undefined;\n      var T;\n      if (typeof mapFn !== \'undefined\') {\n        // 5. else\n        // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.\n        if (!isCallable(mapFn)) {\n          throw new TypeError(\'Array.from: when provided, the second argument must be a function\');\n        }\n        // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.\n        if (arguments.length > 2) {\n          T = arguments[2];\n        }\n      }\n      // 10. Let lenValue be Get(items, "length").\n      // 11. Let len be ToLength(lenValue).\n      var len = toLength(items.length);\n      // 13. If IsConstructor(C) is true, then\n      // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.\n      // 14. a. Else, Let A be ArrayCreate(len).\n      var A = isCallable(C) ? Object(new C(len)) : new Array(len);\n      // 16. Let k be 0.\n      var k = 0;\n      // 17. Repeat, while k < len\xe2\x80\xa6 (also steps a - h)\n      var kValue;\n      while (k < len) {\n        kValue = items[k];\n        if (mapFn) {\n          A[k] = typeof T === \'undefined\' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);\n        } else {\n          A[k] = kValue;\n        }\n        k += 1;\n      }\n      // 18. Let putStatus be Put(A, "length", len, true).\n      A.length = len;\n      // 20. Return A.\n      return A;\n    };\n  }());\n}\n
Run Code Online (Sandbox Code Playgroud)\n