我正在测试我的网站的 I.E 兼容性,但我立即遇到了很多问题:
DOM7011: The code on this page disabled back and forward caching. For more information, see: http://go.microsoft.com/fwlink/?LinkID=291337
webshop.html
HTML1300: Navigation occurred.
webshop.html
SCRIPT1004: Expected ';'
main.js (16,18)
此时我最关心的是 ;没有预料到,因为每次我检查代码时它就在那里。
最佳答案
IE doesn't support for..of
,因此您可以转译代码(例如 with Babel )或使用旧的 forEach
:
this.cartShop.forEach(function(item) {
if (...) { ... }
}, this);
更新:我现在看到在 if
block 内有一个 return
语句来提前退出函数,并且这就是为什么我建议的代码表现不同的原因。我不推荐这种方法,因为它会导致意外的行为和代码重复。通常,使用 break
退出循环。
我看到您首先正在检查该商品是否在购物车中:您可以使用 find
来执行此操作:
var item = this.cartShop.find(function(item) {
return item.name === name && ...
});
if (item) {
item.quantity += quantity;
} else {
item = new this.Item(...);
this.cartShop.push(item);
}
this.saveLocalCart();
不幸的是,find
在 IE 中再次不可用,但与 for..of
相反,您可以简单地提供 polyfill 。或者,您始终可以将旧代码与经典的 for
循环一起使用:
for (var i = 0; i < this.cartShop.length; i++) {
var item = this.cartShop[i];
if (...) { ... }
}
更冗长,可读性较差,但仍然有效。
关于javascript - I.E(JavaScript、jQuery)中预期出现 ';' 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41653426/