最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript實現(xiàn)簡易的計算器功能

 更新時間:2022年03月29日 16:00:35   作者:Of_the  
這篇文章主要為大家詳細介紹了javascript實現(xiàn)簡易的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了javascript實現(xiàn)簡易的計算器的具體代碼,供大家參考,具體內(nèi)容如下

javascript實現(xiàn)簡易計算器,只有兩個input輸入框,簡單實現(xiàn)加減乘除:

<!DOCTYPE html>
<html>
? ? <head>
? ? ? ? <meta charset="UTF-8">
? ? ? ? <title>簡易計算器</title>
? ? </head>
? ? <style>
? ? ? ? body{
? ? ? ? border: 2px #00FFFF; ? ?
? ? ? ? }
? ? </style>
? ? <body ?background="img/2.jpg" style="background-repeat: no-repeat;margin-left: 10px;">
? ? ? ? <p>
? ? ? ? ? ? <h1>簡易計算器</h1>
? ? ? ? ? ? <input type="text" id="num1" width="50px" />
? ? ? ? ? ? <select id="select">
? ? ? ? ? ? ? ? <option value="+">+</option>
? ? ? ? ? ? ? ? <option value="-">-</option>
? ? ? ? ? ? ? ? <option value="*">*</option>
? ? ? ? ? ? ? ? <option value="/">/</option>
? ? ? ? ? ? </select>
? ? ? ? ? ? <input type="text" id="num2" width="50px"/>
? ? ? ? ? ? <br />
? ? ? ? ? ? <input type="button" value="計算" onclick="cal()" style="color: #FF0000;"/>
? ? ? ? ? ? <input id="result"></input>
? ? ? ? </p>
? ? </body>
? ? <script type="text/javascript">
? ? ? ? function cal(){
? ? ? ? ? ? var num1=document.getElementById("num1").value;
? ? ? ? ? ? var num2=document.getElementById("num2").value;
? ? ? ? ? ? var c = document.getElementById("select").value;
? ? ? ? ? ? num1=parseFloat(num1);
? ? ? ? ? ? num2=parseFloat(num2);
? ? ? ? ? ? switch(c){
? ? ? ? ? ? ? ? case "+":
? ? ? ? ? ? ? ? document.getElementById("result").value=parseInt(num1)+parseInt(num2);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "-":
? ? ? ? ? ? ? ? document.getElementById("result").value=parseInt(num1)-parseInt(num2);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "*":
? ? ? ? ? ? ? ? document.getElementById("result").value=parseInt(num1)-parseInt(num2);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "/":
? ? ? ? ? ? ? ? document.getElementById("result").value=parseInt(num1)/parseInt(num2);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
</script>
</html>

效果截圖:

JavaScript eval() 函數(shù)實現(xiàn)計算器:只有一個input輸入框:

<!DOCTYPE html>
<html>
? ? <head>
? ? ? ? <meta charset="UTF-8">
? ? ? ? <title>簡易計算器</title>
? ? </head>
? ? <body background="img/2.jpg" style="background-repeat: no-repeat;margin-left: 10px;">
? ? ? ? <h1>簡易計算器</h1>
? ? ? ? <p>
? ? ? ? ? ? <input ?type="text" id="num1"/>
? ? ? ? ? ? <input type="button" value="計算" onclick="cal()" />
? ? ? ? </p>
? ? ? ? <p>
? ? ? ? ? ? <span id="result" style="color: #FF0000;">計算結(jié)果:</span>
? ? ? ? </p>
? ? </body>
? ? <script type="text/javascript">
? ? ? ? function cal(){
? ? ? ? ? ? var num1=document.getElementById("num1").value;
? ? ? ? ? ? var result=document.getElementById("result");
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? //有值就計算
? ? ? ? ? ? ? ? var res=eval("("+num1+")");
? ? ? ? ? ? ? ? result.innerHTML=res;
? ? ? ? ? ? }catch(e){
? ? ? ? ? ? ? ? console.log(e);
? ? ? ? ? ? ? ? result.innerHTML="表達式異常";
? ? ? ? ? ? }
? ? ? ? }
? ? </script>
</html>

實現(xiàn)效果:

body里面的背景圖片設(shè)置:

background-repeat: no-repeat;圖片原大小
background-size:100%; 圖片全鋪滿

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

自贡市| 南丰县| 乌恰县| 正镶白旗| 河间市| 资阳市| 封丘县| 甘谷县| 汽车| 永康市| 开封县| 南阳市| 罗定市| 绥棱县| 清河县| 朔州市| 河东区| 林口县| 临泉县| 大埔县| 济南市| 衡东县| 襄城县| 法库县| 阳曲县| 湟中县| 沾益县| 迭部县| 嘉义县| 蕲春县| 哈巴河县| 临夏县| 冕宁县| 德钦县| 普兰县| 惠州市| 枝江市| 双峰县| 新田县| 元谋县| 永州市|