include()在IE中不起作用 [英] includes() not working in IE

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

问题描述

我正在使用以下代码.它在Chrome,Firefox或Edge等浏览器中完美运行.但是,当我签入IE11时,它不起作用并破坏了所有功能.

I am using the following code. It works perfectly in browsers such as Chrome, Firefox or Edge. However when I check in IE11 it does not work and breaks all the functionality.

var href = jQuery(this).attr('href');
if (href.includes('#')) {
  // action
}

我认为includes()在IE11中不起作用.有解决方案吗?

I think includes() does not work in IE11. Is there a solution for this?

推荐答案

问题是因为在所有版本的IE中都不支持includes():

The issue is because includes() is unsupported in all versions of IE: MDN Reference

相反,您可以使用受更广泛支持的indexOf():

Instead you can use the much more widely supported indexOf():

var href = $(this).attr('href');
if (href.indexOf('#') != -1) {
  // action
}

您也可以将此原型制作为将includes()方法添加到IE:

You could also prototype this to add the includes() method to IE:

String.prototype.includes = function(match) {
  return this.indexOf(match) !== -1;
}

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

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