向html网页弹出警告框
window.alert()
向html文档输出(写入html标签)
使用语句
document.write()
传入参数向html中输出内容。
注:如果在文档已完成加载后执行 document.write(),整个 HTML 页面将被覆盖。
实例
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="utf-8">
</head>
<body>
<p>This a test</p>
<script>
document.write("<p>I did not cover the page,\"This is a test\" is on my head!</p>");
</script>
<button onclick="document.write('I coverd the page,cause I am called after the document is loaded')">touch me</button>
</body>
</html>
输出到浏览器控制台
使用语句
console.log()
使用innerHTML向html元素插入内容(更改内容)
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<p id="demo">将被替换的内容</p>
<script>
document.getElementById("demo").innerHTML="已被替换" ;
</script>
</body>
</html>