.includes()在Internet Explorer中不起作用 [英] .includes() not working in Internet Explorer

查看:90
本文介绍了.includes()在Internet Explorer中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在Internet Explorer中不起作用.还有其他选择吗?

This code does not work in internet explorer. Any alternative?

"abcde".includes("cd")

在撰写本文时,

推荐答案

String.prototype.includes在Internet Explorer(或Opera)中不受支持.

String.prototype.includes is, as you write, not supported in Internet Explorer (or Opera).

相反,您可以使用String.prototype.indexOf. #indexOf返回子字符串的第一个字符的索引(如果它在字符串中),否则返回-1. (非常类似于Array)

Instead you can use String.prototype.indexOf. #indexOf returns the index of the first character of the substring if it is in the string, otherwise it returns -1. (Much like the Array equivalent)

var myString = 'this is my string';
myString.indexOf('string');
// -> 11

myString.indexOf('hello');
// -> -1

MDN使用indexOfincludes提供了一个polyfill: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill

MDN has a polyfill for includes using indexOf: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill

版本28 开始,Opera支持includes.

Opera supports includes as of version 28.

Edge的当前版本支持该方法. (截至2019年)

EDIT 2: Current versions of Edge supports the method. (as of 2019)

这篇关于.includes()在Internet Explorer中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆