<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
.father {
width: 200px;
height: 200px;
background-color: red;
}
.son {
width: 100px;
height: 100px;
background-color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(function () {
/*
想要自定义事件,必须满足两个条件
1,事件必须是通过on绑定的
2,事件必须通过trigger来触发
想要事件命名空间有效,必须瞒住连个条件
1,事件是通过on来绑定的
2,通过trigger触发事件
注意点:利用trigger出发子元素带命名空间的事件,
父元素的相同命名空间的事件也会被触发
利用trigger触发子元素不带命名空间的事件,那么子元素所有相同类型的
事件,和父元素所有相同类型的时间都会被触发
*/
// $(".son").myClick(function(event){
// alert("son")
// })
$(".son").on("myClick",function(){
alert("son");
})
$(".son").trigger("myClick");
//事件的 命名空间 click.ls click.zs 就是对相同类型的事件的不同吗,命名
$(".son").on("click.ls",function(){
alert("click1");
})
$(".son").on("click.zs",function(){
alert("click1");
})
$(".son").trigger("click.ls");
})
</script>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
<a href="http://www.baidu.com">我是百度</a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
</form>
</body>
</html>
jQuery中怎么自动触发事件,和事件命名空间规则
猜你喜欢
转载自blog.csdn.net/weixin_43342105/article/details/104476541
今日推荐
周排行