网站更新内容:请访问: https://bigdata.ministep.cn/

Javascript中点击(click)事件的3种写法

Javascript中点击(click)事件的3种写法_h5css3_linhuai的博客-CSDN博客_点击

<!DOCTYPE html>
<html>
<head>
	<title>Javascript中点击事件方法一</title>
</head>
<body>
	<button id="btn">click</button>
	<script type="text/javascript">
		var btn = document.getElementById("btn");
		btn.οnclick=function(){
			alert("hello world");
		}
	</script>
</body>
</html>

消除事件:btn.οnclick=null;
方法二:

<!DOCTYPE html>
<html>
<head>
	<title>Javascript中点击事件方法二</title>
</head>
<body>
	<button id="btn">click</button>
	<script type="text/javascript">
		var btn = document.getElementById("btn");
		btn.addEventListener('click',function(){
			alert("hello wrold");
		},false)
	</script>
</body>
</html>

方法三:

<!DOCTYPE html>
<html>
<head>
	<title>Javascript中点击事件方法三</title>
	<script type="text/javascript">
		function test(){
			alert("hello world");
		}
	</script>
</head>
<body>
	<button id="btn" οnclick="test()">click</button>
</body>
</html>
posted @ 2021-11-13 10:20  ministep88  阅读(6352)  评论(0编辑  收藏  举报
网站更新内容:请访问:https://bigdata.ministep.cn/