jQuery的事件绑定

bind()绑定事件
为被选元素添加一个或多个事件处理程序,并规定事件发生时运行的函数。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		
		<script src="jquery-3.4.1.js" type="text/javascript" charset="utf-8"></script>
<!-- 		<script type="text/javascript">
			$(function(){
				//绑定事件(绑定单个事件)
				$("btntest").bind("click",function(){
					console.log("世上无难事,只怕有心人");
				})
				
			});
		</script> -->
	</head>
	<body>
		<h3>bind()方简单的绑定事件</h3>
		 <div id="test" style="cursor:pointer">点击查看名言</div>
		 <input id="btntest" type="button" value="点击就不可用了" />
		 <button type="button" id="1">按钮1</button>
		 <button type="button" id="2">按钮2</button>
		 <button type="button" id="3">按钮3</button>
		 <button type="button" id="4">按钮4</button>
	</body>
	<script type="text/javascript">
		//绑定事件(绑定单个事件)
		$("#test").bind("click",function(){
			console.log("世上无难事,只怕有心人");
		});
		//简单常用版本---平时用这个
		$("#btntest").click(function(){
			console.log("nihao");
		});
		//禁用按钮
		$("#btntest").click(function(){
			$(this).prop("disabled",true);
		});
		//为多个事件绑定同一个函数
		$("#1").bind("click mouseout",function(){
			console.log("按钮1。。。");
		});
		//为元素绑定多个事件,并设置对应的函数
		$("#2").bind("click",function(){
			console.log("第一个函数");
		}).bind("mouseout",function(){
			console.log("第二个函数");
		});
		//为元素绑定多个事件,并设置对应的函数   ----方法2
		$("#3").bind({
			"click":function(){
				console.log("第一个函数");
			},
			"mouseout":function(){
				console.log("第二个函数");
			}
			
		});
		//为元素绑定多个事件,并设置对应的函数  ----方法3 这个是最常用的方法
		$("#4").click(function(){
			console.log("第一个函数");
		}).mouseout(function(){
			console.log("第二个函数");
		});
	</script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值