proprio qui c’è un blocco del mio codice. Funziona perfettamente in fireFox e Chrome. Ma non in IE. Ottengo l’errore “L’ 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)" }) } }
Potrei cambiarlo un po ‘e usare vanilla JS e fare:
document.getElementById("right-tree").classList.contains
Ma preferirei vedere se c’è un modo per farlo funzionare in IE prima di cambiare il JS e modificare HTML e CSS.
Se si guarda la documentazione di includes()
, la maggior parte dei browser non supporta questa proprietà.
È ansible utilizzare ampiamente supportato indexOf()
dopo aver convertito la proprietà in string utilizzando toString()
:
if ($(".right-tree").css("background-image").indexOf("stage1") > -1) { // ^^^^^^^^^^^^^^^^^^^^^^
Puoi anche utilizzare il polyfill da MDN .
if (!String.prototype.includes) { String.prototype.includes = function() { 'use strict'; return String.prototype.indexOf.apply(this, arguments) !== -1; }; }
IE11 implementa String.prototype.include quindi perché non utilizzare Polyfill ufficiale?
Fonte: fonte polyfill
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; } }; }
Un’altra soluzione consiste nell’utilizzare contiene che restituiscono vero o falso
_.contains ($ (“. right-tree”). css (“background-image”), “stage1”)
Spero che questo ti aiuti