简体   繁体   English

有没有办法可以在 function 之外声明变量?

[英]Is there a way that I can declare the variable outside function?

Is there a way that I can declare a variable outside the function?有没有办法可以在 function 之外声明一个变量? $first and $second can't seem to read it outside the function. $first 和 $second 似乎无法在 function 之外读取它。 and 1 more thing .remove() doesn't work.还有 1 件事.remove()不起作用。

function addition(){
  let $first = parseInt($('#firstNum').val());
  let $second = parseInt($('#secondNum').val());
  $('h1').append($first + $second);
}
function subtraction(){
  let $first = parseInt($('#firstNum').val());
  let $second = parseInt($('#secondNum').val());
  $('h1').append($first - $second);
}
function multiplication(){
  let $first = parseInt($('#firstNum').val());
  let $second = parseInt($('#secondNum').val());
  $('h1').append($first * $second);
}
function division(){
  let $first = parseInt($('#firstNum').val());
  let $second = parseInt($('#secondNum').val());
  $('h1').append($first / $second);
}
function clear(){
  $('h1').remove();
}```

1 more thing.remove() doesn't work.还有 1 个 thing.remove() 不起作用。

You should clear empty instead of removing that element:您应该清除空白而不是删除该元素:

$("#btnClear").on("click", function(){
  $('h1').html('');
})

Besides, you should refactor your code like below:此外,您应该重构您的代码,如下所示:

 $("#btnSubmit").on("click", function(){ var firstNumber = parseInt($('#firstNum').val()); var secondNumber = parseInt($('#secondNum').val()); var operator = $("input[name='operator']:checked").val(); var result = 0; switch(operator){ case '+': result = addition(firstNumber, secondNumber); break; case '-': result = subtraction(firstNumber, secondNumber); break; case '*': result = multiplication(firstNumber, secondNumber); break; case '/': result = division(firstNumber, secondNumber); break; } $('h1').html(result); }); function addition(firstNumber, secondNumber){ return firstNumber + secondNumber; } function subtraction(firstNumber, secondNumber){ return firstNumber - secondNumber; } function multiplication(firstNumber, secondNumber){ return firstNumber * secondNumber; } function division(firstNumber, secondNumber){ return firstNumber / secondNumber; } $("#btnClear").on("click", function(){ $('h1').html(''); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id ="firstNum" > <input type="text" id ="secondNum" > <a id="btnSubmit">Submit</a> <br> <a><input name="operator" value = "+" type = "radio" checked/> + </a> <br> <a><input name="operator" value = "-" type = "radio"/> - </a> <br> <a><input name="operator" value = "*" type = "radio"/> * </a> <br> <a><input name="operator" value = "/" type = "radio"/> / </a> <br> Result: <h1></h1> <a id="btnClear">Clear</a>

You need to define your variables outside the functions.您需要在函数之外定义变量。

Variable can be read in the current scope + inner scopes, but not outer scopes from which they are defined in.可以在当前 scope + 内部范围内读取变量,但不能在其中定义它们的外部范围内读取。

let $first, $second;

function addition() {
  $first = parseInt($('#firstNum').val());
  $second = parseInt($('#secondNum').val());
  $('h1').append($first + $second);
}

MDN has a good article explaining scopes in JavaScript: https://developer.mozilla.org/en-US/docs/Glossary/Scope MDN 有一篇很好的文章解释了 JavaScript 中的范围: https://developer.mozilla.org/en-US/docs/Glossary/Scope

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

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