Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用箭头函数重写函数以修复IE兼容性_Javascript_Jquery_Arrow Functions - Fatal编程技术网

Javascript 使用箭头函数重写函数以修复IE兼容性

Javascript 使用箭头函数重写函数以修复IE兼容性,javascript,jquery,arrow-functions,Javascript,Jquery,Arrow Functions,我通常不使用javascript编写,我自己也遇到了一些问题。我编写了以下代码与一些在线捐赠软件进行交互,这样当有人单击捐赠金额按钮时,捐赠页面上的amountNum input[type='radio']就会打印出如果他们支付了处理费,那么捐赠总额是多少。这起作用了。。。在除internet explorer之外的所有浏览器中。原来箭头函数在IE中不起作用,没有它我不知道如何重写函数。有什么建议吗 var inputs = document.querySelectorAll(".amountN

我通常不使用javascript编写,我自己也遇到了一些问题。我编写了以下代码与一些在线捐赠软件进行交互,这样当有人单击捐赠金额按钮时,捐赠页面上的amountNum input[type='radio']就会打印出如果他们支付了处理费,那么捐赠总额是多少。这起作用了。。。在除internet explorer之外的所有浏览器中。原来箭头函数在IE中不起作用,没有它我不知道如何重写函数。有什么建议吗

var inputs = document.querySelectorAll(".amountNum input[type='radio']");

inputs.forEach(el =>
el.addEventListener('click', function() {
  var value = parseInt(this.value, 10);
  var URL = `https://example.com/display-fee?contribution=${value}&max=2500.00`;
  jQuery.get(URL, function(data) {
    var fee = data.fee;
    var total = fee + value;
    total = total.toFixed(2);
    $("#full-gift-label")[0].innerText = ("I'd like to help cover the transaction fees on my donation. My grand total will be £" + total);
  })
}));

在这种情况下,arrow函数的执行方式与标准函数相同——只需将el=>替换为functionel{

但是,还有一个问题:如果你想支持IE,querySelectorAll会返回一个节点列表,并且只有在更新的浏览器上才能直接通过节点列表进行forEach。因此,你必须使用Array.prototype.forEach

除此之外,IE也不支持模板文本,因此请改用普通字符串连接:

var inputs = document.querySelectorAll(".amountNum input[type='radio']");

Array.prototype.forEach.call(
  inputs,
  function(el) {
    el.addEventListener('click', function() {
      var value = parseInt(this.value, 10);
      var URL = 'https://example.com/display-fee?contribution=' + value + '&max=2500.00';
      jQuery.get(URL, function(data) {
        var fee = data.fee;
        var total = fee + value;
        total = total.toFixed(2);
        $("#full-gift-label")[0].innerText = ("I'd like to help cover the transaction fees on my donation. My grand total will be £" + total);
      })
    })
  }
);
在一般情况下,最好使用Babel自动将您的代码从最新和最好的语言版本传输到ES5,这样您就可以最轻松地编写代码,并且让较老的浏览器能够理解它:


你的HTML中有多少个.amountNums?到底是一个还是多个?啊,看,我刚刚尝试重写一个标准函数,但当它不起作用时,可能是因为你标记的forEach问题,我认为还有其他问题在起作用。我现在剩下的问题是IE似乎不喜欢我在var周围使用的backticksURL?啊,仍然是ES6ing。使用了Babel并解决了。非常感谢!是的,这是一个模板文字,是IE中不支持的另一个ES6功能。在这种情况下,您可以只执行var URL=https://example.com/display-fee?contribution=+值+&最大值=2500.00;