javascript输出

javascript打印输出

输出方法

  1. window.alert() 弹窗打印
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>空白</title>
</head>
<body>
	<p id="tx">你是谁? </p>
	<script>
	function my(){
		alert("你到底是谁呢?")
	}
		</script>
		<button type="button" onclick="my()">点击我试试</button>
</body>
</html>
  1. document.write() 写入HTML文档
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>空白</title>
</head>
<body>
	<p id="tx">你是谁? </p>
	<script>
	function my(){
		document.write('你是万能的神!')
	}
		</script>
		<button type="button" onclick="my()">点击我试试</button>
</body>
</html>
  1. innerHTML 写入HTML元素
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>空白</title>
</head>
<body>
	<p id="tx">你是谁? </p>
	<script>
	function my(){
		document.getElementById('tx').innerHTML="你是救世主!"
	}
		</script>
		<button type="button" onclick="my()">点击我试试</button>
</body>
</html>
  1. console.log() 控制台打印
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>空白</title>
</head>
<body>
	<p id="tx">你是谁? </p>
	<script>
	function my(){
		console.log('你是万能的神!')
	}
		</script>
		<button type="button" onclick="my()">点击我试试</button>
</body>
</html>

通过以上打印进行测试代码执行是否正确。

原文地址:https://www.cnblogs.com/huny/p/13532668.html