includes() not working in all browsers

前端 未结 6 1563
不思量自难忘°
不思量自难忘° 2020-11-27 17:24

right here is a block of my code. It works perfect in fireFox and Chrome. But not in IE. I get the error \"Object doesn\'t support property or method \'includes\'

6条回答
  •  醉酒成梦
    2020-11-27 17:55

    If you look at the documentation of includes(), most of the browsers don't support this property.

    You can use widely supported indexOf() after converting the property to string using toString():

    if ($(".right-tree").css("background-image").indexOf("stage1") > -1) {
    //                                           ^^^^^^^^^^^^^^^^^^^^^^
    

    You can also use the polyfill from MDN.

    if (!String.prototype.includes) {
        String.prototype.includes = function() {
            'use strict';
            return String.prototype.indexOf.apply(this, arguments) !== -1;
        };
    }
    

提交回复
热议问题