HTML의 HTML 요소에 이벤트 추가

방법 1 :

코드 예 :

<!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>

방법 2 :

코드 예 :

<!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>

방법 3 :

코드 예 :

<!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>

 

추천

출처blog.csdn.net/m0_46383618/article/details/107427090