Add events to HTML elements in HTML

method one:

Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script>
			function test(){
				console.log("hello world!");
			}
		</script>
	</head>
	<body>
		<input type="button" value="按钮" onclick="test()" />
	</body>
</html>

Method Two:

Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<input type="button" value="按钮" id="button" />
		<script>
			document.getElementById("button").onclick=function(){
				console.log("hello world!");
			}
		</script>	
	</body>
</html>

Method three:

Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<input type="button" value="按钮" id="button" />
		<script>
			document.getElementById("button").addEventListener("click",function(){
				console.log("hello world!");
			})
		</script>
	</body>
</html>

 

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/107427090