我目前正在学习如何编码,我在使用jQuery时遇到了一些困难。我试图通过我正在做的课程将这个计算器作为一个项目,我遇到了一个问题。我不知道如何修复我的代码:一个问题是操作不正常,也没有数字。我尝试了一个教程,并决定使用不同的按钮来玩它,但我没有得到理想的结果。数字应显示在总截面上,操作员应给出数学结果。
你可以在这里看到我的html,jQuery和javascript的完整代码 http://codepen.io/IrvingG/pen/adRrvW
$(document).ready(function() {
var testNumLength = function(number) {
if (number.length > 9) {
totaldiv.text(number.substr(number.length - 9, 9));
if (number.length > 15) {
number = "";
totaldiv.text("Err");
}
}
};
var number = "";
var newnumber = "";
var operator = "";
var totaldiv = $("#results");
totaldiv.text("0");
$(".numbers button").not("#operators, #equals, #clear").click(function() {
number += this.html();
totaldiv.text(number);
testNumLength.text(number);
});
$("#operators").click(function() {
operator += this.html();
newnumber = number;
number = "";
totaldiv.text("0");
});
$("#clear, #clear1").click(function() {
number = "";
totaldiv.text("0");
if ($(this).attr("id") === "#clear") {
newnumber = ";"
}
});
$("#equals").click(function(){
if (operator === "+"){
number = (parseInt(number, 10) + parseInt(number, 10).toString(10));
} else if (operator === "-"){
number = (parseInt(number, 10) - parseInt(number, 10).toString(10));
} else if (operator === "/"){
number = (parseInt(number, 10) / parseInt(number, 10).toString(10));
} else if (operator === "*"){
number = (parseInt(number, 10) * parseInt(number, 10).toString(10));
}
totaldiv.text(number);
testNumLength(number);
number="";
newnumber="";
});
});
答案 0 :(得分:0)
以下是代码Calculator
$(document).ready(function() {
var testNumLength = function(number) {
if (number.length > 9) {
totaldiv.text(number.substr(number.length - 9, 9));
if (number.length > 15) {
number = "";
totaldiv.text("Err");
}
}
};
var result;
var number = "";
var newnumber = "";
var operator = "";
var totaldiv = $("#results");
totaldiv.text("0");
$(".numbers button").not(".operator,#equals ,#clear").click(function() {
number += $(this).html();
totaldiv.text(number);
testNumLength(number);
});
$(".operator").click(function() {
operator = $(this).html();
newnumber = number;
number = "";
totaldiv.text("0");
});
$("#clear, #clear1").click(function() {
number = "";
totaldiv.text("0");
if ($(this).attr("id") === "#clear") {
newnumber = ";";
}
});
$("#equals").click(function(){
if (operator === "+"){
result=parseInt(newnumber, 10) + parseInt(number, 10);
number = result.toString(10);
} else if (operator === "-"){
result=parseInt(newnumber, 10) - parseInt(number, 10);
number = result.toString(10);
} else if (operator === "/"){
result=parseInt(newnumber, 10) / parseInt(number, 10);
number = result.toString(10);
} else if (operator === "*"){
result=parseInt(newnumber, 10) * parseInt(number, 10);
number = result.toString(10);
}
totaldiv.text(number);
testNumLength(number);
number="";
newnumber="";
});
});