jQuery事件绑定与解绑

一、事件绑定与解绑

1、click()-->单击事件(监听)

2、on()-->绑定事件

3、mouseenter()-->鼠标进入

4、mouseleave()-->鼠标离开

5、off()-->关闭事件

6、bind()-->绑定事件

7、event.stopPropagation()-->阻止冒泡

8、one()-->绑定事件(只能执行一次)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			*{
				margin: 0px;
			}
			.out{
				position: absolute;
				width: 200px;
				height: 200px;
				top: 20px;
				left: 10px;
				background-color: aqua;
			}
			.inner{
				position: absolute;
				width: 100px;
				height: 100px;
				top: 50px;
				background-color: red;
			}
			.out1{
				position: absolute;
				width: 200px;
				height: 200px;
				top: 300px;
				left: 10px;
				background-color: aqua;
			}
			.inner1{
				position: absolute;
				width: 100px;
				height: 100px;
				top: 50px;
				background-color: red;
			}
			.out2{
				position: absolute;
				width: 200px;
				height: 200px;
				top: 600px;
				left: 10px;
				background-color: aqua;
			}
			.inner2{
				position: absolute;
				width: 100px;
				height: 100px;
				top: 50px;
				background-color: red;
			}
		</style>
		<script src="js/jquery-1.7.2.min.js"></script>
		<script>
			$(function(){
				/* 监听 */
				$(".out").click(function(){
					alert("click")
				});
				/* 绑定*/
				$(".out").on("click",function(){
					alert("on")
				});
				$(".inner").mouseenter(function(){
					console.log("进入");
				});
				$(".inner").mouseleave(function(){
					console.log("离开");
				});
				/* 关闭事件 */
				$(".inner").off();
				/* 绑定  可以绑定多个事件 */
				$(".out1").bind("mouseenter",function(){
					console.log("进入");
				});
				/* 事件冒泡 */
				$("#btn").click(function(event){
					console.log("按钮点击");
					event.stopPropagation();//阻止冒泡
				});
				$(".divbtn").click(function(){
					console.log("div点击");
				});
				/* one 只能执行一次的函数 */
				$(".out2").one("click",function(){
					console.log("one事件");
				});
			});
		</script>
	</head>
	<body>
		<div class="out">
			外部div
			<div class="inner">
				内部div
			</div>
		</div>
		<div class="divbtn">
			<button id="btn">我是按钮</button>
		</div>
		<div class="out1">
			外部div1
			<div class="inner1">
				内部div1
			</div>
		</div>
		<div class="out2">
			外部div2
			<div class="inner2">
				内部div2
			</div>
		</div>
	</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪&秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值