简易计算器实现优化

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>calculator-小太阳</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>


</head>
<style type="text/css">
* { padding: 0; margin: 0; }
li { list-style: none; }
#counter { 500px; height: 420px; position: relative; }
#counter h2 { line-height: 42px; padding-left: 15px; font-size: 14px; font-family: arial; color:#FFF; }
#counter a {font-weight: normal; text-decoration: none; color:#FFF; }
#counter a:hover {text-decoration: underline; }
#bg {  280px; height: 200px; border: 3px solid #004f69; background: #FFFFFF; filter: alpha(opacity=80); opacity: 0.8; position: absolute; left: 50%; top: 115px; margin-left:-141px;}
#counter_content {  250px; position: absolute; top: 130px; left: 130px; z-index: 1; }
#counter_content h3 { margin-bottom: 10px; }
#counter_content h3 input {   223px; height: 30px; line-height: 30px; padding: 0 10px; background: url(images/ico.png) no-repeat; text-align: right; color: #333; font-size: 14px; font-weight: bold; }
#counter_content ul {  250px; }
#counter_content li {  60px; height: 30px; line-height: 30px; float: left; background: url(images/ico.png) no-repeat -303px 0; text-align: center; color: #fff; cursor: pointer; margin: 0 1px 4px 0; }
#counter_content .active { background: url(images/ico.png) no-repeat -244px 0; }
#counter p {  500px; position: absolute; bottom: 20px; left: 0; color: #FFF; text-align: center; font-size: 12px; }
</style>
<body>

<div id="counter">
    
    <div id="counter_content">
        <h3><input id="input1" type="text" value="0" /></h3>
        <ul>
            <li>7</li>
            <li>8</li>
            <li>9</li>
            <li>+</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>-</li>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>×</li>
            <li>0</li>
            <li>C</li>
            <li>=</li>
            <li>÷</li>
        </ul>
    </div>
    <div id="bg"></div>
    <p>power by dingyue </p>
</div>
</body>
</html>
<script type="text/javascript">
/* andy Ding */
var counter =(function(){
    var sNum1='';
    var val='';
    //是否需要清除输入框中已有的内容
    var bNeedClear=false;
    var count = function(iNum1, iNum2, val){
        var iResult=0;
        switch(val){
            case '×':
                iResult=iNum1*iNum2;
                break;
            case '+':
                iResult=iNum1+iNum2;
                break;
            case '-':
                iResult=iNum1-iNum2;
                break;
            case '÷':
                iResult=iNum1/iNum2;
                break;
            default:
                iResult=iNum2;
        }
        
            return iResult;
    };
    var doInput= function(){
        var oInput=document.getElementById('input1');
        //alert(""+this.innerHTML);
        var sHtml=this.innerHTML.replace(' ','');
        switch(sHtml){
            case '=':
                oInput.value=count(parseInt(sNum1), parseInt(oInput.value), val);
                sNum1='';
                val='';
                bNeedClear=true;
                break;
            case '+':
            case '-':
            case '×':
            case '÷':
                bNeedClear=true;
                if(sNum1.length!=0){
                    oInput.value=count(parseInt(sNum1), parseInt(oInput.value), val);
                }
                val=sHtml;
                sNum1=oInput.value;
                break;
            case 'C':
                oInput.value='0';
                sNum1='';
                val='';
                break;
            default:    //数字
                if(bNeedClear){
                    oInput.value=parseInt(sHtml, 10);
                    bNeedClear=false;
                }
                else{
                    oInput.value=parseInt(oInput.value+sHtml, 10);
                }
                break;
            }
    };
    var init = function(){
        var aLi=document.getElementsByTagName('li');
        var i=0;
        for(i=0;i<aLi.length;i++){
            aLi[i].onmousedown=doInput;
            aLi[i].onmouseover=function ()
            {
                this.className='active';
            };
            
            aLi[i].onmouseout=function ()
            {
                this.className='';
            };
        }
    };

    return {
        init:init
    }
})();
counter.init();
</script>
<!-- 计算器原生写法  -->
相信才会最好
原文地址:https://www.cnblogs.com/dingyue/p/7099202.html