aquí hay un bloque de mi código. Funciona perfecto en firefox y chrome. Pero no en IE. Recibo el error " Object doesn't support property or method 'includes' "
function rightTreeSwapfunc2() { if ($(".right-tree").css("background-image").includes("stage1") == true) { $(".right-tree").css({ backgroundImage: "url(/plant-breeding/img/scenes/plant-breeding/stage5.jpg)" }) } else { $(".right-tree").css({ backgroundImage: "url(/plant-breeding/img/scenes/plant-breeding/stage3.jpg)" }) } }Podría cambiarlo un poco y usar Vanilla JS y hacer:
document.getElementById("right-tree").classList.contains
Pero preferiría ver si hay una manera de hacer que funcione en IE antes de cambiar el JS y editar el HTML y el CSS.
Si observa la documentación de includes() , la mayoría de los navegadores no admiten esta propiedad.
Puede usar indexOf() ampliamente compatible después de convertir la propiedad en una cadena usando toString() :
if ($(".right-tree").css("background-image").indexOf("stage1") > -1) { // ^^^^^^^^^^^^^^^^^^^^^^También puedes usar el polyfill de MDN .
if (!String.prototype.includes) { String.prototype.includes = function() { 'use strict'; return String.prototype.indexOf.apply(this, arguments) !== -1; }; }IE11 implementa String.prototype.includes, entonces, ¿por qué no usar el Polyfill oficial?
Fuente: fuente de relleno polivinílico
if (!String.prototype.includes) { String.prototype.includes = function(search, start) { if (typeof start !== 'number') { start = 0; } if (start + search.length > this.length) { return false; } else { return this.indexOf(search, start) !== -1; } }; }En mi caso encontré mejor usar "string.search".
var str = "Some very very very long string"; var n = str.search("very");Por si le sirve a alguien.