Javascript 文本输入计算器

标签 javascript html

我对 Javascript 有点陌生,我正在尝试制作一个基本的计算器,它有 3 个文本输入、第一个数字文本框、一个操作文本框和第二个数字文本框,但它不会打印出文本当我单击按钮或使用任何其他方法触发事件时。 这是我的代码:

<html>
<script>
function calc()
{
    var D = "";
    var A = document.getElementById("num1").value;
    var B = document.getElementById("op").value;
    var C = document.getElementById("num2").value;
    if(B == "+")
    {
        D = A+C;
    }
    elseif(B == "-")
    {
        D = A-C;
    }
    elseif(B == "*")
    {
        D = A*C;
    }
    elseif(B == "/")
    {
        D = A/C;
    }
    document.getElementById("result").innerHTML = D;
}
</script>

<body>
    <input type="text" id="num1" name="num1" />
    <input type="text" id="op" name="op" />
    <input type="text" id="num2" name="num2" />
    <br />
    <input type="button" value="Solve" onclick="calc()" />

    <p id="result" name="r1">
        <br />
    </p>

</body>
</html>

最佳答案

我建议如下(代码本身注释的解释):

function calc() {
        /* finds out whether the browser uses textContent (Webkit, Opera, Mozilla...)
           or innerText (Microsoft) to set the text of an element/node */
    var textType = Node.textContent ? 'textContent' : 'innerText',
        /* uses parseFloat to create numbers (where possible) from the entered value
           if parseFloat fails to find a number (it's empty or nonsensical)
           then a 0 is used instead (to prevent NaN being the output). */
        num1 = parseFloat(document.getElementById('num1').value) || 0,
        num2 = parseFloat(document.getElementById('num2').value) || 0,
        // retrieves the result element
        result = document.getElementById('result');

    // switch is used to avoid lots of 'if'/'else if' statements,
    // .replace() is used to remove leading, and trailing, whitespace
    // could use .trim() instead, but that'd need a shim for (older?) IE
    switch (document.getElementById('op').value.replace(/\s/g,'')){
        // if the entered value is:
        // a '+' then we set the result element's text to the sum
        case '+':
            result[textType] = num1 + num2;
            break;
        // and so on...
        case '-':
            result[textType] = num1 - num2;
            break;
        case '*':
            result[textType] = num1 * num2;
            break;
        case '/':
            result[textType] = num1 / num2;
            break;
        // because people are going to try, give a default message if a non-math
        // operand is used
        default:
            result[textType] = 'Seriously? You wanted to try math with that operand? Now stop being silly.'
            break;
    }
}

JS Fiddle demo .

引用资料:

关于Javascript 文本输入计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15994510/

相关文章:

javascript - 需要在 Internet Explorer 脚本错误中查找行/字符的实际位置?

javascript - 删除数组对象中的重复项

javascript - 带提示的两个数字的总和

javascript - 可拖动图标未显示在 Google map 顶部且不重复

html - IE和FireFox中div的高度百分比

javascript - 为什么 jquery 看不到 html 的变化?

javascript - 尝试将此代码包装在命名函数中

javascript - 在 map 功能中传递 Prop 不起作用。将其作为 key 传递

javascript - 仅当窗口大小最小化时才加载 Jquery 动画(Fiddle 已更新)

html - 面包屑未在更改屏幕方向时居中对齐