56

我一直在做一个项目并开发一个 JavaScript 框架。原始代码大约 700 行,所以我只粘贴了这一行。include 方法在 Internet Explorer 上不起作用。有什么解决办法吗?

var row_cells = tbl_row.match(/<td[\s\S]*?<\/td>/g);

    row.Cells = new Array();
    if (onRowBindFuncText != null) { /*Fonksyon tanımlanmaışsa daha hızlı çalış*/

        var cellCount = 0;
        for (i = 0; i < row_cells.length; i++) {

            var cell = new Cell();
            $.each(this, function (k, v) {

                if ((row_cells[i]+"").includes("#Eval(" + k + ")")) {

                    cell.Keys.push(new Key(k,v));

...代码继续

4

8 回答 8

75

因为 IE 不支持,所以 Opera 也不支持(请参阅兼容性表),但您可以使用建议的polyfill

填充物

此方法已添加到 ECMAScript 2015 规范中,可能尚未在所有 JavaScript 实现中可用。但是,您可以轻松地填充此方法:

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}
于 2015-07-04T13:50:25.303 回答
48

@Infer-on 给出了很好的答案,但在特定情况下存在问题。如果您使用 for-in 循环,它将返回包含您添加的“包含”函数。

这是另一个 pollyfill。

if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, "includes", {
    enumerable: false,
    value: function(obj) {
        var newArr = this.filter(function(el) {
          return el == obj;
        });
        return newArr.length > 0;
      }
  });
}
于 2016-09-28T10:09:13.110 回答
11

您可以只使用 .search() > -1 ,它的行为方式完全相同。 http://www.w3schools.com/jsref/jsref_search.asp

if ((row_cells[i]+"").search("#Eval(" + k + ")") > -1) {
于 2016-07-07T16:50:00.383 回答
7

这个选定的答案适用于字符串,如果您正在寻找数组上的“包含”,我通过将以下内容添加到我的 polyfills.ts 文件中解决了我在 Angular 项目中的问题:

import 'core-js/es7/array';
于 2018-06-26T15:00:22.187 回答
4

这是 TypeScript 项目的 polyfill,取自https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/includes并修改为有效的 TypeScript:

if (!Array.prototype.includes) {
    Object.defineProperty(Array.prototype, 'includes', {
        value: function(searchElement, fromIndex) {

            if (this == null) {
                throw new TypeError('"this" is null or not defined');
            }

            const o = Object(this);
            // tslint:disable-next-line:no-bitwise
            const len = o.length >>> 0;

            if (len === 0) {
                return false;
            }
            // tslint:disable-next-line:no-bitwise
            const n = fromIndex | 0;
            let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

            while (k < len) {
                if (o[k] === searchElement) {
                    return true;
                }
                k++;
            }
            return false;
        }
    });
}
于 2018-10-16T05:38:04.807 回答
1
if (fullString.indexOf("partString") >= 0) {
//true 

} else {
//false
}
于 2019-04-17T14:53:23.563 回答
0
var includes = function(val, str) {
  return str.indexOf(val) >= 0;
};
于 2020-05-04T17:32:20.467 回答
0

jquery 有一个解决方案:

if ($.inArray(val,ar)===-1){
    console.log ("val not found in ar");
}
else{
    console.log ("val found in ar");
}

$.inArray(val,ar,[startingIndex]) 函数。

于 2021-07-19T15:26:36.560 回答