一、绑定事件的方法
HTML结构:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jq事件的绑定</title>
<style type="text/css">
#Box {
margin: 0 auto;
width: 330px;
height: 330px;
background: blue;
}
li:nth-child(even) {
background: red;
}
li:nth-child(odd) {
background: green;
}
</style>
</head>
<body>
<div id="Box">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
</body>
</html>
1.通过on()绑定单个事件:
<script type="text/javascript">
$(document).ready( function() {
$('li').on('click', function(){
console.log('触发了点击事件')
console.log(this)
console.log(this.innerHTML)
console.log($(this).text())
})
})
</script>
2.通过on()绑定多个事件:
$('li').on({
click:function(){
console.log('点击了');
},
mouseenter:function(){
console.log('触发了mouseenter');
}
})
3.直接绑定事件
$('li').click(function(){
console.log('点击了');
})
4.进行链式操作
$('li').click(function(){
console.log('点击了');
}).mouseenter(function(){
console.log('触发了mouseenter');
}).mouseleave(function(){
console.log('触发了mouseleave');
})
5.解除事件
$('li').off('click')