旧浏览器无法在javascript中识别

时间:2020-12-19 09:06:19

I'm building a calculator with Foundation range slider. It's working fine in Safari 9, latest Chrome and Firefox for both Mac and Windows, Edge 14. But it doesn't work in Safari 8 or IE 11. In Safari 8, I see an error SyntaxError: Invalid character: '`' The javascript code looks like this:

我正在用Foundation range滑块构建一个计算器。它在Safari 9、最新的Chrome和Firefox中运行良好,适用于Mac和Windows, Edge 14。但在Safari 8或IE 11中不能用。在Safari 8中,我看到一个错误SyntaxError: Invalid字符:' javascript代码如下所示:

Foundation.Move(moveTime, $hndl, function() {
  //adjusting the left/top property of the handle, based on the percentage calculated above
  $hndl.css(lOrT, `${movement}%`);

  if (!_this.options.doubleSided) {
    //if single-handled, a simple method to expand the fill bar
    _this.$fill.css(hOrW, `${pctOfBar * 100}%`);
  } else {
    //otherwise, use the css object we created above
    _this.$fill.css(css);
  }
});

If I change ` to ', it doesn't work in any browser. Anyone has any idea? Thanks!

如果我将“改为”,它在任何浏览器中都不起作用。任何人有任何想法吗?谢谢!

1 个解决方案

#1


3  

If you have to support older browsers, your only real choice is to not use template literals. It is a relatively new feature in JavaScript, and so older browsers will not understand how to interpret it.

如果您必须支持旧的浏览器,那么您唯一的选择就是不使用模板文字。它是JavaScript中一个相对较新的特性,所以老的浏览器不会理解如何解释它。

Here it the link to the documentation which shows it's support:

这里是文档链接,显示了它的支持:

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

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

You can always to back to the "old fashioned way":

你总是可以回到“老式的方式”:

this

 `${movement}%`

becomes this:

变成这样:

 movement + "%"

#1


3  

If you have to support older browsers, your only real choice is to not use template literals. It is a relatively new feature in JavaScript, and so older browsers will not understand how to interpret it.

如果您必须支持旧的浏览器,那么您唯一的选择就是不使用模板文字。它是JavaScript中一个相对较新的特性,所以老的浏览器不会理解如何解释它。

Here it the link to the documentation which shows it's support:

这里是文档链接,显示了它的支持:

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

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

You can always to back to the "old fashioned way":

你总是可以回到“老式的方式”:

this

 `${movement}%`

becomes this:

变成这样:

 movement + "%"