这段javascript代码可以在chrome中工作,但不能在firefox中工作

This piece of javascript code is working in chrome but not in firefox

本文关键字:工作 但不能 firefox javascript 代码 这段 chrome      更新时间:2023-09-26

我用Jquery和Javascript创建了一个计算器,在Chrome中一切都很好,但在Firefox中却不行。

function validate(x, y, z) {
if ((isNaN(x) || x == "") || (isNaN(y) || y == "")) {
return "Please Enter A Valid Number";
} else if (z == "/" && y == 0) {
return "Divide By zero error";
} else if (event.keyCode == 32) {
return false;
} else {
return calculation(x, y, z);
}
}

此处演示

不要依赖window.event。这是一种微软主义,并非所有浏览器都能实现。但是,每个处理程序都将接收作为参数的事件。明确使用:

$(document).ready(function() {
  $("#first").focus();
  $('#first,#second,#oper').keyup(function(evt) {
    // HERE                                ^^^
    $('#demo').html(validate(evt.keyCode, $('#first').val(), $('#second').val(), $('#oper').val()));
    // and HERE              ^^^^^^^^^^^
  });
});
function validate(key, x, y, z) {
  // and HERE     ^^^
  if ((isNaN(x) || x == "") || (isNaN(y) || y == "")) {
    return "Please Enter A Valid Number";
  } else if (z == "/" && y == 0) {
    return "Divide By zero error";
  } else if (key == 32) {
    return false;
  } else {
    return calculation(x, y, z);
  }
}

链接

          $(document).ready(function() {
          $("#first").focus();
          $('#first,#second,#oper').keyup(function(event) {
            $('#demo').html(validate(event,$('#first').val(), $('#second').val(), $('#oper').val()));
          });
        });
        function validate(event,x, y, z) {
          if ((isNaN(x) || x == "") || (isNaN(y) || y == "")) {
            return "Please Enter A Valid Number";
          } else if (z == "/" && y == 0) {
            return "Divide By zero error";
          } else if (event.keyCode == 32) {
            return false;
          } else {
            return calculation(x, y, z);
          }
        }
相关文章: