<script type="text/javascript">
var f;
var fuhao;
function out(num) {
document.getElementById("txtword").value += num;// 吧相应的数字显示在文本框中
}
function outer(fu) { //返回相应的运算符号,用以在等号里判断
f = document.getElementById("txtword").value;
document.getElementById("txtword").value = "";
fuhao = fu;
return fuhao;
}
function clear1() { // 清除所有数据!
document.getElementById("txtword").value = "";
}
function subss() {
var t = document.getElementById("txtword");
t.value = t.value.substring(0, t.value.length - 1);
}
function equal() { //等号里的判断!
if (fuhao == '+') {
document.getElementById("txtword").value = parseFloat(f) + parseFloat(document.getElementById("txtword").value);
}
else if (fuhao == '-') {
document.getElementById("txtword").value = parseFloat(f) - parseFloat(document.getElementById("txtword").value);
}
else if (fuhao == '*') {
document.getElementById("txtword").value = parseFloat(f) * parseFloat(document.getElementById("txtword").value);
}
else if (fuhao == '/') {
document.getElementById("txtword").value = parseFloat(f) /parseFloat(document.getElementById("txtword").value);
}
}
</script>
本文介绍了一个使用JavaScript实现的简易计算器的功能,包括数字输入、四则运算、清除操作及回退等功能。通过具体的代码示例,展示了如何处理用户输入并进行基本的数学计算。
557

被折叠的 条评论
为什么被折叠?



