javascript – ‘.contains’在Android手机中不起作用,但它在网页上运行良好

我有一个产品列表,我想通过输入参数来scarch产品列表.所以我使用contains来scarching产品列表中的输入字符串.它与网页的工作正常.但是当我在移动网页中打开同一页面时,它无法正常工作.并在未定义时给出’包含’的错误.

if(productlist[i].name.toLowerCase().contains(input_val.toLowerCase()))

    --my business logic--

之后我尝试使用indexOf,然后在两种情况下都能正常工作.

if(productlist[i].name.toLowerCase().indexOf(input_val.toLowerCase()) !== -1)
    --my business logic --

.contains的问题是什么?

最佳答案 使用此Polyfill,(参考:
MDN)

if(!('contains' in String.prototype)) {
  String.prototype.contains = function(str, startIndex) { 
      return -1 !== String.prototype.indexOf.call(this, str, startIndex); 
  };
}

请参阅Compatibility table我可以使用…

更新:您也可以查看this answer.

点赞