老师,为什么这段计算器代码只能正常显示而无法实现数学运算呢?

来源:4-4 第三次修改--提取函数

weixin_慕前端6235132

2020-02-18 19:41:05

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>简易计算器</title>
<style>
body {
background-color: #eee;
}
#calculator {
margin: 100px 0 0 150px;
}
</style>
</head>
<body>

<!-- 简易计算器 -->
<div id="calculator">
<p>
<input type="text" class="formerInput" value="1" />
<span class="sign">+</span>
<input type="text" class="laterInput" value="1" />
<span>=</span>
<span class="resultOutput">2</span>
</p >
<p>
<input type="button" value="+" class="btn" title="add" />
<input type="button" value="-" class="btn" title="subtract" />
<input type="button" value="×" class="btn" title="multiply" />
<input type="button" value="÷" class="btn" title="divide" />
<input type="button" value="%" class="btn" title="mod" />
<input type="button" value="^" class="btn" title="power" />
<input type="button" value="1/x" class="btn" title="invert" />
</p >
</div>

<script>
// 绑定事件
each(calculatorElem.btns, function (index, elem ) {
elem.onclick = function () {
updateSign(this.value);
outputResult(operate(this.title, calculatorElem.formerInput.value, calculatorElem.laterInput.value));
};
});
</script>
</body>
</html>

写回答

1回答

好帮手慕粉

2020-02-19

同学你好,当老师运行同学的代码时,报如下错误:

http://img.mukewang.com/climg/5e4c98f009af0e2307450132.jpg

each函数未定义,查看同学的代码发现同学只实现了样式和html结构,没有写js代码,所以逻辑实现不了,同学是把老师的源码直接粘贴过来然后把注释的删掉了吗?建议同学听一下课,跟着老师练一下代码,就知道这是分很多步的代码,老师把游泳的代码也注释掉了。

同学可以再回顾下课程,跟着老师敲下代码。

如果我的回答帮助了你,欢迎采纳,祝学习愉快~

0

0 学习 · 14456 问题

查看课程