使用html,css,javascript,通过jQuery实现计算器功能


前言

今天学习了html,css,javascript的一些基础知识
简单实现一个计算器的基本功能

  • 清零

以下是本篇文章正文内容,下面案例可供参考

通过jQuery实现计算器功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
    <script src="jquery-1.9.1.min.js"></script>
    <script>
        function calc(type) {
            var inputNum1 = jQuery("#num1");
            var inputNum2 = jQuery("#num2");
            if(type==1){
                if (inputNum1.val()=="") {
                    alert("请先输入数字1");
                    return false;
                }
                if (inputNum2.val()=="") {
                    alert("请先输入数字2");
                    return false;
                }
                // 加法
                var total = parseInt(inputNum1.val()) + parseInt(inputNum2.val());
                //jQuery("#resultDiv").html(
                //  "<h2>计算相加的结果为:<strong style='color: chocolate;'>" + total + "</strong></h2>");
                jQuery("#resultDiv")[0].innerHTML=
                    "<h2>计算相加的结果为:<strong style='color: chocolate;'>" + total + "</strong></h2>";
            }
            else if(type==2){
                if (inputNum1.val()=="") {
                    alert("请先输入数字1");
                    return false;
                }
                if (inputNum2.val()=="") {
                    alert("请先输入数字2");
                    return false;
                }
                // 减法
                var total = parseInt(inputNum1.val()) - parseInt(inputNum2.val());
                jQuery("#resultDiv")[0].innerHTML=
                    "<h2>计算相减的结果为:<strong style='color: chocolate;'>" + total + "</strong></h2>";
            }
            else if(type==3){
                if (inputNum1.val()=="") {
                    alert("请先输入数字1");
                    return false;
                }
                if (inputNum2.val()=="") {
                    alert("请先输入数字2");
                    return false;
                }
                // 乘法
                var total = parseInt(inputNum1.val()) * parseInt(inputNum2.val());
                jQuery("#resultDiv")[0].innerHTML=
                    "<h2>计算相乘的结果为:<strong style='color: chocolate;'>" + total + "</strong></h2>";
            }
            else if(type==4){
                if (inputNum1.val()=="") {
                    alert("请先输入数字1");
                    return false;
                }
                if (inputNum2.val()=="") {
                    alert("请先输入数字2");
                    return false;
                }
                // 乘法
                var total = parseInt(inputNum1.val()) / parseInt(inputNum2.val());
                jQuery("#resultDiv")[0].innerHTML=
                    "<h2>计算相除的结果为:<strong style='color: chocolate;'>" + total + "</strong></h2>";
            }
            else if(type==5){
                if(confirm("是否确认清空")) {
                    // 清空数据
                    inputNum1.val("");
                    inputNum2.val("");
                    jQuery("#resultDiv")[0].innerHTML="";
                }
            }
        }
    </script>
</head>
<body>
<div style="text-align: center;margin-top: 100px;">
    <h1>计算器</h1><p></p>
    数字1<input id="num1" type="number"><p></p>
    数字2<input id="num2" type="number"><p></p>
    <div>
        <input type="button" value=" 相 加 " onclick="calc(1)">
        <input type="button" value=" 相 减 " onclick="calc(2)">
        <input type="button" value=" 相 乘 " onclick="calc(3)">
        <input type="button" value=" 相 除 " onclick="calc(4)">
        <input type="button" value=" 清 空 " onclick="calc(5)">
    </div>
    <div id="resultDiv" style="margin-top: 50px;">
    </div>
</div>
</body>
</html>

最终呈现

在这里插入图片描述
注意这里只简单贴了相加功能的图片


总结

  1. 在写的过程中,遇到jquery无法调用innerHTML的问题,原生js写法为(以本文为例):document.getElementById("resultDiv").innerHTML,查询资料得知jquery没有innerHTML这个属性,要使用jQuery("#resultDiv")[0].innerHTML语句,或者jQuery("#resultDiv").html();语句。一开始看到之后不知道为什么要加[0],查后得知是要对jquery对象和js对象进行转换。
  2. jquery对象与dom对象的转换
    只有jquery对象才能使用jquery定义的方法。dom对象和jquery对象是有区别的,普通的dom对象一般可以通过jQuery()转换为jquery。
    eg:jQuery(document.getElementById("resultDiv"))
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值