博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用HTML,CSS和JavaScript构建技巧计算器
阅读量:2519 次
发布时间:2019-05-11

本文共 2049 字,大约阅读时间需要 6 分钟。

A Tip Calculator is a calculator that calculates a tip based on the percentage of the total bill.

小费计算器是根据总账单的百分比计算小费的计算器。

Let's build one now.

让我们现在建立一个。

第1步-HTML: (Step 1 - HTML:)

We create a form in order to enter the preferred amount:

我们创建一个表格以输入首选金额:

      Tip Calculator    

Tip Calculator

$

Results
Tip amount
Total Bill with Tip

第2步-CSS: (Step 2 - CSS:)

You design the style however you want. You can also use CSS to hide the results and show them through JavaScript after the user fills in the form:

您可以根据需要设计样式。 您还可以使用CSS隐藏结果,并在用户填写表单后通过JavaScript显示结果:

#results {         display:none;       }

步骤3:JavaScript: (Step 3: JavaScript:)

We add an onchange event. The onchange event occurs when the user interacts with the form.

我们添加一个onchange事件。 当用户与表单交互时,会发生onchange事件。

This action will execute a function that computes the final bill amount based on the percentage tip, then returns the results.

该操作将执行一个函数,该函数根据百分比提示计算最终的帐单金额,然后返回结果。

document.querySelector('#tip-form').onchange = function(){  var bill = Number(document.getElementById('billTotal').value);  var tip = document.getElementById('tipInput').value;  document.getElementById('tipOutput').innerHTML = `${tip}%`;  var tipValue = bill * (tip/100)  var finalBill = bill + tipValueconsole.log(finalBill)var tipAmount = document.querySelector('#tipAmount')var totalBillWithTip = document.querySelector('#totalBillWithTip')tipAmount.value = tipValue.toFixed(2); totalBillWithTip.value =finalBill.toFixed(2); //Show Results  document.getElementById('results').style.display='block'}

You can see a working example and its code on .

您可以在上看到一个有效的示例及其代码。

翻译自:

转载地址:http://qqrwd.baihongyu.com/

你可能感兴趣的文章
结构体指针
查看>>
迭代器
查看>>
Food HDU - 4292 (结点容量 拆点) Dinic
查看>>
Ubuntu安装Sun JDK及如何设置默认java JDK
查看>>
[经典算法] 排列组合-N元素集合的M元素子集
查看>>
Codeforces 279D The Minimum Number of Variables 状压dp
查看>>
打分排序系统漫谈2 - 点赞量?点赞率?! 置信区间!
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>