显示方案
代码 | 说明 |
---|---|
window.alert() | 写入警告框 |
document.write() | 写入 HTML 输出 |
innerHTML | 写入 HTML 元素 |
onsole.log() | 写入浏览器控制台 |
innerHTML
document.getElementById(id)
<!DOCTYPE html>
<html>
<body>
<h1>我的第一张网页</h1>
<p>我的第一个段落</p>
<p id="demo"></p> //定义hmtl的ID
<script>
document.getElementById("demo").innerHTML = 5 + 6; //查询id为"demo"的标签
</script>
</body>
</html>
document.write()
主要用于测试
<!DOCTYPE html>
<html>
<body>
<h1>我的第一张网页</h1>
<p>我的第一个段落</p>
<script>
document.write(5 + 6); //直接邮件正文显示运算结果11
</script>
</body>
</html>
文档加载完,将删除已有的html
<!DOCTYPE html>
<html>
<body>
<h1>我的第一张网页</h1>
<p>我的第一个段落</p>
<button onclick="document.write(5 + 6)">试一试</button> // 点击后输出结果并删除html
</body>
window.alert()
<!DOCTYPE html>
<html>
<body>
<h1>我的第一张网页</h1>
<p>我的第一个段落</p>
<script>
window.alert(5 + 6);
</script>
</body>
console.log()
显示数据 请通过 F12 来激活浏览器控制台,并在菜单中选择“控制台”。
<!DOCTYPE html>
<html>
<body>
<h1>我的第一张网页</h1>
<p>我的第一个段落</p>
<script>
console.log(5 + 6);
</script>
</body>
</html>