JS+CSS实现小计算器

本文介绍如何利用HTML和JavaScript构建一个基本的计算器界面,包括输入框、按钮和简单的数学运算逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<head>
<style>
body
{
    color:#004c75;
}
.button
{
    height:25px;
    width:25px;
    background-color:#7abaff;
    margin:1px 1px 1px 1px;
    float:left;
    text-align:center;
    border:1px solid #ccdef2;
}
.panel
{
    width:110px;
    height:161px;
    margin:auto;
    border:1px solid #5f91c7;
    background-color:#004c75;
    margin-top:80px;
}
.res
{
    width:110px;
    height:25px;
    border:1px solid #5f91c7;
    background-color:#fffeab;
}
</style>
<script>
function Y(a,xn)
{
    var fxn = xn*xn - a;
    return fxn;
}
function K(xn)
{
    var kxn = 2*xn;
    return kxn;
}
function Rec(xn,fxn,kxn)
{
    xn = xn - (fxn/kxn);
    return xn;
}
//牛顿迭代
function Newton()
{
    var xn = 5;
     
    var a = document.getElementById("scr").value;
    if(a == "")
    {a = 0;return a;}
    var fxn;
    var kxn;
    for(i =0;i<10;i++)
    {
        fxn = Y(a,xn);
        kxn = K(xn);
        xn = Rec(xn,fxn,kxn);
    }
    document.getElementById("scr").value = xn;
     
}
function Back()
{
    var s = document.getElementById("scr").value;
    l = s.length-1;
 
    document.getElementById("scr").value = s.slice(0,l);
 
}
function Squr()
{
    document.getElementById("scr").value *= document.getElementById("scr").value;
}
function AddStr(x)
{
    document.getElementById("scr").value += x;
}
function Cln()
{
document.getElementById("scr").value = "";
}
function Res()
{
     
    var r = document.getElementById("scr").value;
    if(r == "")
    {   r = 0;
    return ;}
    r = eval(r);
    document.getElementById("scr").value = r;
}
var x = 1;
var k = 42;
//绘制计算器
document.write("<div class='panel'>");
document.write("<input id='scr' type='text' class='res'>");
document.write("</input>");
document.write("<button class = 'button' >"+String.fromCharCode(32)+"</button>");
 
document.write("<button class = 'button'  onClick='Newton()'>"+String.fromCharCode(8730)+"</button>");
document.write("<button class = 'button' onClick='Back()' >"+String.fromCharCode(8592)+"</button>");
document.write("<button class = 'button' onClick='Cln()'>"+String.fromCharCode(67)+"</button>");
for(i = 0;i < 3;i++)
{
    document.write("<div>");
    for(j = 0;j < 3;j++)
    {
         
        document.write("<button class = 'button' onClick='AddStr("+x+")'>"+x+"</button>");
         
        x++;
    }
    if(i<2)
    {document.write("<button class = 'button' onClick='AddStr(""+String.fromCharCode(k)+"")'>"+String.fromCharCode(k)+"</button>");}
    else {document.write("<button class = 'button' onClick='AddStr(""+String.fromCharCode(45)+"")'>"+String.fromCharCode(45)+"</button>");}
    k++;
    document.write("</div>");
}
document.write("<button class = 'button' onClick='AddStr(""+String.fromCharCode(48)+"")'>"+String.fromCharCode(48)+"</button>");
document.write("<button class = 'button' onClick='AddStr(""+String.fromCharCode(46)+"")'>"+String.fromCharCode(46)+"</button>");
document.write("<button class = 'button' onClick='Res()'>"+String.fromCharCode(61)+"</button>");
document.write("<button class = 'button' onClick='Squr(""+String.fromCharCode(178)+"")'>x"+String.fromCharCode(178)+"</button>");
 
 
 
 
document.write("</div>");
</script>
</head>
<body>
<script>
 
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值