我正在尝试调试与 IE11 不兼容的 JS:
Object doesn't support property or method 'includes'
我读到过 includes
可以很容易地替换为 indexof
。但是,由于我的 JS 知识非常有限,我不确定这是否是我需要在这里进行的唯一更改...或者其他部分是否也需要重写?
原创
if (el.method_title.includes('|')){
el.timeslot = el.method_title.split('|')[1].trim();
el.method_title = el.method_title.split('|')[0].trim();
}
提案
if (el.method_title.indexOf('|')){
最佳答案
该方法称为 indexOf
,如果在您的字符串中未找到子字符串,将返回 -1
,因此您应该进行比较:
if (el.method_title.indexOf('|') >= 0)
indexOf()
方法返回指定值在字符串中第一次出现的位置。
如果要搜索的值从未出现,则此方法返回 -1
。
注意:indexOf() 方法区分大小写。
关于javascript - 我可以简单地用 Javascript 中的 Indexof 替换掉 Includes 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55367011/