在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML5  HTML/ jquery計(jì)算器思路

jquery計(jì)算器思路

寫個(gè)計(jì)算器,沒(méi)有思路,大哥們幫幫忙?。?!
我是利用class給所有的按鈕添加點(diǎn)擊事件呢還是根據(jù)ID一個(gè)個(gè)添加,如何用class給所有按鈕添加,那么后面該怎么辦!

    <!--第一排-->
    <input type="button" id="7" value="7" class="buttons"/>
    <input type="button" id="8" value="8" class="buttons"/>
    <input type="button" id="9" value="9" class="buttons"/>
    <input type="button" id="Back" value="Back" class="buttons"/>
    <input type="button" id="C" value="C" class="buttons" style="margin-right:0px"/>
    <!--第二排-->
    <input type="button" id="4" value="4" class="buttons"/>
    <input type="button" id="5" value="5" class="buttons"/>
    <input type="button" id="6" value="6" class="buttons"/>
    <input type="button" id="*" value="x" class="buttons"/>
    <input type="button" id="/" value="/" class="buttons" style="margin-right:0px"/>
    <!--第三排-->
    <input type="button" id="1" value="1" class="buttons"/>
    <input type="button" id="2" value="2" class="buttons"/>
    <input type="button" id="3" value="3" class="buttons"/>
    <input type="button" id="+" value="+" class="buttons"/>
    <input type="button" id="-" value="-" class="buttons" style="margin-right:0px"/>
    <!--第四排-->
    <input type="button" id="0" value="0" class="buttons"/>
    <input type="button" id="00" value="00" class="buttons"/>
    <input type="button" id="." value="." class="buttons"/>
    <input type="button" id="%" value="%" class="buttons"/>
    <input type="button" id="eva" value="=" class="buttons" style="margin-right:0px"/>
回答
編輯回答
法克魷

class加呀

 <div class="box">
            <p></p>
            <p></p>
</div>
        
         var btns = document.querySelectorAll('input.buttons'),
        text = document.querySelectorAll('.box p'),
        arr = [],
        Result;
        [].slice.call(btns).forEach(function(element) {
            element.onclick = function(){                                     
               if(this.value === '='){
                Result = eval('('+arr.join('')+')');
                arr  = []; 
                text[1].innerText = Result;  
               }else{
                    text[0].innerText = '';
                    if(this.value === 'C'){
                        arr = [];
                        return; 
                    }    
                    if(this.value === 'Back'){
                        arr.pop();
                    }else{
                        arr.push(this.value);
                    }                                             
                    arr.map(function(num){
                        text[0].innerText += num;
                    });
               }
            }
        });

有其余的特殊鍵,你判斷一下this.value處理一下就行了

2018年3月12日 22:54
編輯回答
單眼皮

原生js和jquery并不沖突啊,jquery只是輔助工具,樓上的老哥提供的代碼很棒,如果要用jquery可以這么寫

var btns=$("input .class");
btns.click(function(event) {
 console.log(event.currentTarget.val());
});
2017年4月21日 13:15
編輯回答
熊出沒(méi)

我給你html添加了兩個(gè)顯示的span

輸入的字符:<span class="txt"></span><br/>
結(jié)果:<span class="Result"></span><br/>

$(function() {
        $(".buttons").click(function () {
            var $value = this.value;
            var str = $(".txt").html();
            if ($value=="=") {
                getResult();
                return;
            }
            if ($value == "Back") {
                $(".txt").html(str.substr(0, str.length - 1));
                return;
            }
            if ($value == "C") {
                $(".txt").html("");
                return;
            }
            $(".txt").html(str + $value);
        });
    });
    function getResult() {
        $(".Result").html(eval($(".txt").html()));
    }

大概就是這樣了 不確定有什么bug 對(duì)了 你把X的value改為*吧 不然不識(shí)別

2018年8月13日 02:50