Event binding
Simple operation of jquery event:
$().Event type(function Event handling);
$().Event type();
##1. jquery event binding
<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> //同一个对象绑定多个click事件 $(function(){ $('div').bind('click',function(){ console.log('谁在碰我?'); }); $('div').bind('click',function(){ console.log('谁又在碰我?'); }); $('div').bind('mouseover',function(){ //给div设置背景色 $(this).css('background-color','lightgreen'); }); $('div').bind('mouseout',function(){ //给div设置背景色 $(this).css('background-color','lightblue'); }); }); </script> <style type="text/css"> div {width:300px; height:200px; background-color:lightblue;} </style> </head> <body> <div></div> </body> </html>1.2 Cancel event binding ① $().unbind(); //Cancel all events② $().unbind(event type); //Cancel events of the specified type ③ $().unbind(event type, processing); //Cancel the specified processing event of the specified typeNote: For the ③ type of cancellation of event binding, the event processing must be a named function.
<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> //unbind()取消事件绑定操作 //以下f1和f2要定义到最外边,使用没有任何影响 function f1(){ console.log(1111); } function f2(){ console.log(2222); } $(function(){ $('div').bind('click',function(){ console.log('谁在碰我?'); }); $('div').bind('click',function(){ console.log('谁又在碰我?'); }); $('div').bind('click',f1); $('div').bind('click',f2); $('div').bind('mouseover',function(){ //给div设置背景色 $(this).css('background-color','lightgreen'); //$('div').css('background-color','lightgreen'); }); $('div').bind('mouseout',function(){ //给div设置背景色 $(this).css('background-color','lightblue'); //$('div').css('background-color','lightgreen'); }); }); function cancel(){ //取消事件绑定 $('div').unbind(); //取消全部事件 } </script> <style type="text/css"> div {width:300px; height:200px; background-color:lightblue;} </style> </head> <body> <div></div> <input type="button" value="取消" onclick="cancel()"> </body> </html>Event binding is just a form of rich event operations.