简体   繁体   中英

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

So I have this really basic function with a for loop. It runs fine on modern Chrome and Firefox browsers, but not on a particularly picky Firefox 38 browser. According to the docs this function has been supported since 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:

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. Use for (var e of es) instead.

According to MDN, the let statement did not get support in Firefox until version 44.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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