java script计算数字

1. 使用 innerHTML

如需访问 HTML 元素,JavaScript 可使用 document.getElementById(id) 方法。

id 属性定义 HTML 元素。innerHTML 属性定义 HTML 内容:

范例

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

2. 使用 document.write()

出于测试目的,使用 document.write() 比较方便:

范例

<script>
document.write(5 + 6);
</script>

3. 使用 window.alert()

您能够使用警告框来显示数据:

范例

<script>
window.alert(5 + 6);
</script>

4. 使用 console.log()

在浏览器中,您可使用 console.log() 方法来显示数据。

请通过 F12 来激活浏览器控制台,并在菜单中选择“控制台”。

范例

<script>
console.log(5 + 6);
</script>