移除事件方法:
off(events,[selector],[fn])
不带参数:移除元素上绑定的所有事件
Demo:
<!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>
.outer{
width:200px;
height:200px;
background-color: pink;
}
.bc{
background-color: green;
}
</style>
<script src="../../js/jquery.min.js"></script>
<script>
$(function(){
/*
移除事件方法:
off(events,[selector],[fn])
不带参数:移除元素上绑定的所有事件
*/
$('.outer').on('mouseenter mouseleave',function(){
$(this).toggleClass('bc');
});
//事件委派:on()方法
$('ul.data').on('click','li',function(){
alert('li click event!');
});
//移除指定的事件
$('.outer').off('mouseleave');
//移除所有绑定的事件
// $('.outer').off();
//移除事件委派,绑定的事件
$('ul.data').off('click','li');
//绑定一次性事件处理
$('ul.news li').one('click',function(){
alert('test demo');
});
});
</script>
</head>
<body>
<div class="box">
<input type="button" value="点我试试">
</div>
<div class="outer">
</div>
<ul class="data">
<li>1111111</li>
<li>222222</li>
<li>333333</li>
<li>44444</li>
<l>555555</l>
</ul>
<ul class="news">
<li>test demo</li>
</ul>
</body>
</html>