html怎么读取输入框的值并进行运算

 时间:2026-04-23 17:36:54

1、html:

<h1>计算两个值之和</h1>

<input type="text" value=""/>

<span>+</span>

<input type="text" value=""/>

<span>=</span>

<span class="total"></span>

<button class="btn" onclick="total()">计算</button>

html怎么读取输入框的值并进行运算

2、css:

h1{

   text-align: center;

   line-height: 20px;

   font-size: 16px;

}

input {

   width: 30%;

   height: 20px;

   line-height: 20px;

   border: 1px solid #2994CA;

}

span{

   display: inline-block;

   width: 10%;

   text-align: center;

}

.total{

   color: red;

}

.btn{

   width: 20%;

   background: #2994CA;

   color: #fff;

   margin: 20px auto;

   display: block;

   height: 30px;

   border-radius: 5px;

}

html怎么读取输入框的值并进行运算

3、js:

function total(){

   var number = 0;

   //获取所有的input,遍历获取值,进行相加

   $("input").each(function(){

       number += parseFloat($(this).val());

   });

   //将相加之和,显示到相应的位置

   $(".total").html(number)

}

html怎么读取输入框的值并进行运算

  • 怎样用setInterval和setTimeout实现显示时间?
  • php怎么连接数据库
  • python中设计类的三要素是什么
  • vscode在哪里设置鼠标光标动画
  • devc++如何导入visualc++项目
  • 热门搜索
    服役是什么意思 放量下跌意味着什么 女人吃维生素e功效 qt是什么 人生像什么 什么牌子手机好 nc是什么 投桃报李是什么意思 科技是什么 永日念什么