用HTML和javascript制作在线计算器
我想制作一个在线计算器,我想,我可以使用HTML中的表单,我制作了一些表单,但没有什么问题,我需要从(input type=“number”name=“first number”)和(input type=“number”name=“second number”)中获取值,然后将它们放入脚本等式中,然后将结果写入(input type=“text”)当用户输入文件编号并按下提交按钮时用HTML和javascript制作在线计算器,javascript,html,forms,var,equation,Javascript,Html,Forms,Var,Equation,我想制作一个在线计算器,我想,我可以使用HTML中的表单,我制作了一些表单,但没有什么问题,我需要从(input type=“number”name=“first number”)和(input type=“number”name=“second number”)中获取值,然后将它们放入脚本等式中,然后将结果写入(input type=“text”)当用户输入文件编号并按下提交按钮时 卡尔库拉契卡 扎德吉·兹德·普夫尼西斯洛 + - x : 扎德吉·兹德·德鲁赫西斯洛 výsledek: 变
卡尔库拉契卡
扎德吉·兹德·普夫尼西斯洛
+
-
x
:
扎德吉·兹德·德鲁赫西斯洛
výsledek:
变量a,文本;
y=
x=
a=y+x
文本=+a;
文件。书写(文本);
变量b,文本;
y=
x=
b=y-x
文本=+b;
文件。书写(文本);
变量c,文本;
y=
x=
c=y*x
text=+c;
文件。书写(文本);
变量d,文本;
y=
x=
d=y/x
text=+d;
文件。书写(文本);
这里有一个小示例,说明如何使用document.getElementById()
从输入元素读取,并使用按钮中的onclick
事件处理程序写入span
元素
A:
B:
算计
A*B=
我真的不明白你的问题,也不明白你想做什么
但是,要获取输入字段的数据,需要执行DOM访问:
document.getElementsByTagName('vysledek')[0].value = "test1234";
您需要通过以下方式回复:
<code>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
width: 130px;
}
#CLEAR {
width: 150px;
}
button {
width: 45px;
}
</style>
<script>
function myFunction(clickedId) {
document.calc.result.value+=clickedId;
}
function Clear() {
document.calc.result.value="";
}
function compute() {
try{
var inp=eval(document.calc.result.value);
document.calc.result.value=inp;
}catch(err){
document.calc.result.value="Bad Input!!";
}
}
</script>
</head>
<body>
<form name="calc">
<input type="text" name="result" readonly>
</form>
<table >
<tr>
<td><button type="button" id="1" onclick="myFunction(this.id)">1</button></td>
<td><button type="button" id="2" onclick="myFunction(this.id)">2</button></td>
<td><button type="button" id="3" onclick="myFunction(this.id)">3</button></td>
</tr>
<tr>
<td><button type="button" id="4" onclick="myFunction(this.id)">4</button></td>
<td><button type="button" id="5" onclick="myFunction(this.id)">5</button></td>
<td><button type="button" id="6" onclick="myFunction(this.id)">6</button></td>
</tr>
<tr>
<td><button type="button" id="7" onclick="myFunction(this.id)">7</button></td>
<td><button type="button" id="8" onclick="myFunction(this.id)">8</button></td>
<td><button type="button" id="9" onclick="myFunction(this.id)">9</button></td>
</tr>
<tr>
<td><button type="button" id="0" onclick="myFunction(this.id)">0</button></td>
<td><button type="button" id="+" onclick="myFunction(this.id)">+</button></td>
<td><button type="button" id="-" onclick="myFunction(this.id)">-</button></td>
</tr>
<tr>
<td><button type="button" id="*" onclick="myFunction(this.id)">*</button></td>
<td><button type="button" id="/" onclick="myFunction(this.id)">/</button></td>
<td><button type="button" id="ANS" onclick="compute()">ANS</button></td>
</tr>
</table>
<button type="button" id="CLEAR" onclick="Clear()">CLEAR</button>
</body></html>
</code>
也许你应该先读一本JavaScriptDOM教程。比如这个
致意
Dustin我开发了一个简单的计算器,它使用Java Scrip eval。这种方法可能会对您有所帮助
你可以在这里在线测试
表,th,td{
边框:1px纯黑;
宽度:130px;
}
#清楚的{
宽度:150px;
}
钮扣{
宽度:45px;
}
函数myFunction(单击EDID){
document.calc.result.value+=点击edid;
}
函数Clear(){
document.calc.result.value=“”;
}
函数compute(){
试一试{
var inp=eval(document.calc.result.value);
document.calc.result.value=inp;
}捕捉(错误){
document.calc.result.value=“输入错误!!”;
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9
0
+
-
*
/
ANS
清楚的
您是否应该使用数据-
属性而不是id
属性来确定按钮的值(在这种情况下,我甚至可以看到正在使用的元素的值,即this.innerText
)?我认为使用id
属性来实现这一点不是一个好主意,这是一个很好的观点。我会试试的。我对javascript还不熟悉,所以我只是想让它工作起来。
document.getElementsByTagName('vysledek')[0].value = "test1234";
<code>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
width: 130px;
}
#CLEAR {
width: 150px;
}
button {
width: 45px;
}
</style>
<script>
function myFunction(clickedId) {
document.calc.result.value+=clickedId;
}
function Clear() {
document.calc.result.value="";
}
function compute() {
try{
var inp=eval(document.calc.result.value);
document.calc.result.value=inp;
}catch(err){
document.calc.result.value="Bad Input!!";
}
}
</script>
</head>
<body>
<form name="calc">
<input type="text" name="result" readonly>
</form>
<table >
<tr>
<td><button type="button" id="1" onclick="myFunction(this.id)">1</button></td>
<td><button type="button" id="2" onclick="myFunction(this.id)">2</button></td>
<td><button type="button" id="3" onclick="myFunction(this.id)">3</button></td>
</tr>
<tr>
<td><button type="button" id="4" onclick="myFunction(this.id)">4</button></td>
<td><button type="button" id="5" onclick="myFunction(this.id)">5</button></td>
<td><button type="button" id="6" onclick="myFunction(this.id)">6</button></td>
</tr>
<tr>
<td><button type="button" id="7" onclick="myFunction(this.id)">7</button></td>
<td><button type="button" id="8" onclick="myFunction(this.id)">8</button></td>
<td><button type="button" id="9" onclick="myFunction(this.id)">9</button></td>
</tr>
<tr>
<td><button type="button" id="0" onclick="myFunction(this.id)">0</button></td>
<td><button type="button" id="+" onclick="myFunction(this.id)">+</button></td>
<td><button type="button" id="-" onclick="myFunction(this.id)">-</button></td>
</tr>
<tr>
<td><button type="button" id="*" onclick="myFunction(this.id)">*</button></td>
<td><button type="button" id="/" onclick="myFunction(this.id)">/</button></td>
<td><button type="button" id="ANS" onclick="compute()">ANS</button></td>
</tr>
</table>
<button type="button" id="CLEAR" onclick="Clear()">CLEAR</button>
</body></html>
</code>