js計算機(複製成html檔案即可用)

來源:互聯網
上載者:User

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0035)http://l21.yunpan.cn/lk/QvevyNkkunxSP -->
<!-- 交流QQ群:26651479 -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>計算機</title>
    <META http-equiv='content-type' content="text/html; charset=utf-8"/>
<style type="text/css">
.btn {height:41px; width:67px; font-size:30px; font-weight:bolder;}
.text{height:41px; width:100%; font-size:23px; font-weight:bolder;}
</style>
</head>
<body>
    <table align="center">
        <tr><td colspan="4" style="text-align: center;"><input class="text" id='Text1' type="text"/></td></tr>
        <tr>
            <td><input class='btn' title="1" type="button" value="1"/></td>
            <td><input class='btn' title="2" type="button" value="2"/></td>
            <td><input class='btn' title="3" type="button" value="3"/></td>
            <td><input class='btn' title="加" type="button" value="+"/></td>
        </tr>
        <tr>
            <td><input class='btn' title="4" type="button" value="4"/></td>
            <td><input class='btn' title="5" type="button" value="5"/></td>
            <td><input class='btn' title="6" type="button" value="6"/></td>
            <td><input class='btn' title="減" type="button" value="-"/></td>
        </tr>
        <tr>
            <td><input class='btn' title="7" type="button" value="7"/></td>
            <td><input class='btn' title="8" type="button" value="8"/></td>
            <td><input class='btn' title="9" type="button" value="9"/></td>
            <td><input class='btn' title="乘" type="button" value="*"/></td>
        </tr>
        <tr>
            <td><input class='btn' title="小數點" type="button" value="."/></td>
            <td><input class='btn' title="0" type="button" value="0"/></td>
            <td><input class='btn' title="計算結果" type="button" value="=" onclick="ev()" /></td>
            <td><input class='btn' title="除" type="button" value="/"/></td>
        </tr>
        <tr>
            <td><input class='btn' title='開根號' type="button" value="√" onclick="SQRT()" /></td>
            <td><input class="btn" title='平方' type="button" value="²" onclick="Pow()" /></td>
            <td><input class="btn" title='清空' type="button" value="C" onclick="clean()" /></td>
            <td><input class="btn" title='刪除一位' type="button" value="←" onclick="del()" /></td>
        </tr>
    </table>

    <div>運算記錄:</div>
    <textarea style="width:98%" rows="15" id="logText"></textarea>
    註:輸入框可鍵盤輸入,也可用小鍵盤點擊輸入。輸入框內按斷行符號或者“=”會自動計算結果<br/>
    交流QQ群:26651479
</body>
</html>

<script language="javascript" type="text/javascript">
// 輸入框
var inputText = document.getElementById('Text1');
// 運算記錄
var logText = document.getElementById('logText');

// 給文字框賦值
function addText(event) {
    event = event || window.event;
    var element = event.target || event.srcElement;
    inputText.value += element.value;
    // 設定焦點
    inputText.focus();
}

// 計算文字框內的運算式
function ev() {
    var value = inputText.value;
    if (value) {
        try {
            var result = eval(value);
            // 顯示結果
            inputText.value = result;

            // 記錄結果
            logText.value = value + '=' + result + '\n' + logText.value;
        }
        catch (e) {
            alert(e.description);
        }
    }
    // 設定焦點
    inputText.focus();
}

//把文字框內的內容清空
function clean() {
    inputText.value = '';
    // 設定焦點
    inputText.focus();
}

//開根號
function SQRT() {
    inputText.value = Math.sqrt(inputText.value);
    // 設定焦點
    inputText.focus();
}

//平方
function Pow() {
    inputText.value = Math.pow(inputText.value, 2);
    // 設定焦點
    inputText.focus();
}

//刪除一位
function del() {
    var value = inputText.value;
    if (value) inputText.value = value.substring(0, value.length - 1);
    // 設定焦點
    inputText.focus();
}

// 添加事件
(function() {
    var elements = document.getElementsByTagName('input');
    for (var i=0, len=elements.length; i<len; i++) {
        var element = elements[i];
        // 按鈕, 點擊事添加一個符號到輸入框(有 onclick 事件的表示有自己獨特的事件,這裡不處理)
        if (element.type == 'button' && !element.onclick) {
            element.onclick = addText;
        }
        // 輸入框, 輸入“=”號或者按斷行符號, 則計算結果
        else if (element.type == 'text') {
            element.onkeydown = function(event) {
                event = event || window.event;
                var currentKey = event.keyCode || event.charCode;
                // 13:斷行符號鍵, “=”號在Firefox是61,在IE是187
                if (13==currentKey || (event.shiftKey === false && (61==currentKey || 187==currentKey))) {
                    ev();
                    return false;
                }
                return true;
            }
        }
    }
    // 設定焦點
    inputText.focus();
})();
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.