javascript - 如何在 HTML 中打印 JavaScript 函数的返回值?
问题描述
我正在创建一个代码,用户可以在其中计算矩形的面积。在提示时输入高度和宽度后,站点应在表格中打印高度、宽度和面积值。高度和宽度代码可以正常工作和打印,但该区域不会打印。我哪里错了?
这是单独的 JavaScript 代码。
var height = prompt("Please enter the height");
var width = prompt("Please enter the width");
function calcArea(height, width) {
var area = height * width;
return area;
}
这是 JavaScript 输出编码的 HTML 代码。
<table>
<caption>Calculate the Area of a Rectangle</caption>
<tr><td class="makert">Height:</td>
<td class="makectr"> <script>document.write(height);</script></td></tr>
<tr><td class="makert">Width:</td>
<td class="makectr"> <script>document.write(width);</script></td></tr>
<tr><td class="makert">Area:</td>
<td class="makectr"> <script>document.write(area);</script></td></tr>
</table>
解决方案
你应该使用你的功能:
<script>document.write(calcArea(height, width));</script>
声明函数的目的是为了以后使用它。
推荐阅读
- mysql - 将 CountCols 函数与生成的列一起使用
- java - 如何在 IntelliJ 中为编辑器设置检查配置文件?
- r - 如何获取有关已识别点的信息并将其排除在 R 的下一个图中
- spring-integration - 为什么使用控制总线发送消息时spring集成的errorChannel中没有捕获到异常
- mysql - 使用查询执行请求 Eclipse 时 BIRT 出错
- apache-spark - 通过 Spark 的 Hive JDBC 连接(Nullpointer 异常)
- node.js - 从节点js中的树数组构建平面数组
- python - 如何在散景中点击获取 x 坐标和 y 坐标
- android - Google 的 ARCore 是否/可以在 Kindle 上运行?
- sqlite - 更改表 SQLLITE