Javascript初学者-有人能告诉我为什么我的按钮不';不行?
JSFIDLE在这里--> 下面是一个有效的示例: 关于第二个JSFIDLE(不是我的),我不太明白为什么他的工作没有在按钮上使用document.getElementById 我觉得我的问题在于点击功能本身:Javascript初学者-有人能告诉我为什么我的按钮不';不行?,javascript,Javascript,JSFIDLE在这里--> 下面是一个有效的示例: 关于第二个JSFIDLE(不是我的),我不太明白为什么他的工作没有在按钮上使用document.getElementById 我觉得我的问题在于点击功能本身: document.getElementById('mainButton').onclick = function () { 谢谢:)正在工作: 您有document.getElementByID,它应该是document.getElementByID 及 您没有像numberOne=
document.getElementById('mainButton').onclick = function () {
谢谢:)正在工作:
您有document.getElementByID
,它应该是document.getElementByID
及
numberOne=parseInt(numberOne,10)那样将字符串转换为int代码>其中10
是十进制的基数李>
你有一个question==ADD
应该是question==ADD'
现在工作:
您有document.getElementByID
,它应该是document.getElementByID
及
您没有像numberOne=parseInt(numberOne,10)那样将字符串转换为int代码>其中10
是十进制的基数李>
你有一个question==ADD
应该是question==ADD'
您的代码有三个问题:
它是getElementById
而不是getElementById
(区分大小写)
您没有将从输入中获得的字符串转换为数字。您可以通过在document.getElementById('number1').value前面添加+
来轻松实现这一点代码>
您是在对变量而不是字符串进行比较。例如question==ADD
而不是question==“ADD”
请参见更正的
document.getElementById('mainButton').onclick = function () {
//Getting values of inputs and saving them to variables
var numberOne = +document.getElementById('number1').value;
var numberTwo = +document.getElementById('number2').value;
//Setting values of the equations
var addition = (numberOne + numberTwo);
var subtraction = (numberOne - numberTwo);
var multiplication = (numberOne * numberTwo);
var division = (numberOne / numberTwo);
//Prompting user for their desired equation
var question = prompt("Do you wish to ADD, SUBTRACT, MULTIPLY, or DIVIDE?").toUpperCase();
//If statement to show the proper equation based on the user's prior prompt
if (question == 'ADD') {
alert('I added the shit out of those numbers for you - turns out it is ' + addition);
} else if (question == 'SUBTRACT') {
alert('Did some of this, some of that, some minusing - your answer is ' + subtraction);
} else if (question == 'MULTIPLY') {
alert('Yeah, I multipled the numbers, big whoop, wanna fight abouddit? the answers over there --> ' + multiplication);
} else if (question == 'DIVIDE') {
alert('This ones my favorite, I love a good division - ' + division);
};
};
您的代码有三个问题:
它是getElementById
而不是getElementById
(区分大小写)
您没有将从输入中获得的字符串转换为数字。您可以通过在document.getElementById('number1').value前面添加+
来轻松实现这一点代码>
您是在对变量而不是字符串进行比较。例如question==ADD
而不是question==“ADD”
请参见更正的
document.getElementById('mainButton').onclick = function () {
//Getting values of inputs and saving them to variables
var numberOne = +document.getElementById('number1').value;
var numberTwo = +document.getElementById('number2').value;
//Setting values of the equations
var addition = (numberOne + numberTwo);
var subtraction = (numberOne - numberTwo);
var multiplication = (numberOne * numberTwo);
var division = (numberOne / numberTwo);
//Prompting user for their desired equation
var question = prompt("Do you wish to ADD, SUBTRACT, MULTIPLY, or DIVIDE?").toUpperCase();
//If statement to show the proper equation based on the user's prior prompt
if (question == 'ADD') {
alert('I added the shit out of those numbers for you - turns out it is ' + addition);
} else if (question == 'SUBTRACT') {
alert('Did some of this, some of that, some minusing - your answer is ' + subtraction);
} else if (question == 'MULTIPLY') {
alert('Yeah, I multipled the numbers, big whoop, wanna fight abouddit? the answers over there --> ' + multiplication);
} else if (question == 'DIVIDE') {
alert('This ones my favorite, I love a good division - ' + division);
};
};
首先,它是getElementById
不getElementById
(区分大小写)。首先,它是getElementById
不getElementById
(区分大小写)。@doniyor-是的。我刚刚注意到你编辑了你的答案,包括parseInt
。使用时不要忘记基数。等等,那么使用+可以节省parseint的时间吗?就javascript实际上在做什么而言,它只是在+?前面的任何东西上运行parseint函数,在javascript中称为一元加号。看,还有@doniyor-是的。我刚刚注意到你编辑了你的答案,包括parseInt
。使用时不要忘记基数。等等,那么使用+可以节省parseint的时间吗?就javascript实际上在做什么而言,它只是在+?前面的任何东西上运行parseint函数,在javascript中称为一元加号。看到了,非常感谢!!有了你和另一个人的回答,我真的明白了:)@user3912585很高兴能帮上忙。别忘了接受我们的回答,非常感谢!!有了你和另一个人的回答,我真的明白了:)@user3912585很高兴能帮上忙。别忘了接受我们的一个答案