简体   繁体   English

为什么Firefox在这个javascript for循环中抱怨分号?

[英]Why is Firefox complaining about a semicolon in this javascript for loop?

So I have this really basic function with a for loop. 所以我有一个非常基本的函数与for循环。 It runs fine on modern Chrome and Firefox browsers, but not on a particularly picky Firefox 38 browser. 它在现代Chrome和Firefox浏览器上运行良好,但在特别挑剔的Firefox 38浏览器上却没有。 According to the docs this function has been supported since Firefox 13. 根据文档,自Firefox 13以来一直支持此功能。

function showhide_class(cl) {
  var es = document.getElementsByClassName(cl);
  for(let e of es) {
  e.style.display = (e.style.display == "block") ? "none" : "block";
  }
}

The exact error being reported by Firefox is: Firefox报告的确切错误是:

SyntaxError: missing ; after for-loop initializer

So, why is this error being reported and do you know of a work around? 那么,为什么要报告这个错误,你知道一个解决方法吗? Thanks so much. 非常感谢。

The let statement is the problem. let语句就是问题所在。 Use for (var e of es) instead. for (var e of es)代替。

According to MDN, the let statement did not get support in Firefox until version 44. 根据MDN的说法, let语句在版本44之前没有得到Firefox的支持。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM