ie不支持includes_include()在IE中不起作用

bd96500e110b49cbb3cd949968f18be7.png

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

}

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

解决方案

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

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

var href = $(this).attr('href');

if (href.indexOf('#') != -1) {

// action

}

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

String.prototype.includes = function(match) {

return this.indexOf(match) !== -1;

}


版权声明:本文为weixin_28197941原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。