jQuery普通事件操作
普通事件操作
dom1層級事件設定
<input ?type=”text”??onclick=”過程性代碼”?value='tom'?/>
#<input ?type=”text”??onclick=”函數(shù)()”?/>
itnode.onclick = function(){}
- ##itnode.onclick = function(){}
dom2級事件設定
- itnode.addEventListener(類型,處理,事件流);
- itnode.removeEventListener(類型,處理,事件流);
- node.attachEvent();
#jquery事件設定
- ##$ ().事件類型(事件處理函數(shù)fn); ?? //設定事件
- #$().事件型別(); ?????????????? ?//觸發(fā)
- #事件類型:click、keyup、keydown、mouseover、mouseout、blur、focus等等
- 例如:$('div').click(function(){事件觸發(fā)過程this});
註:此方式事件函數(shù)內(nèi)部this都代表jquery物件內(nèi)部的dom節(jié)點物件。
<!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> $(function(){ //頁面加載完畢給div綁定事件 $('div').click(function(){ console.log('誰在碰我呢'); }); //$('li').each(function(){ //this關(guān)鍵字分別代表每個li的dom對象 //jquery使用時,代碼結(jié)構(gòu)類似這樣的,this都代表dom對象 //}); $('div').mouseover(function(){ //this.style.backgroundColor = "blue"; //$(this)使得this的dom對象變?yōu)閖query對象 $(this).css('background-color','red'); }); }); function f1(){ //間接觸發(fā)對象的事件執(zhí)行 $('div').click(); //使得div的單擊事件執(zhí)行 $('div').mouseover(); //鼠標移入事件執(zhí)行 } </script> <style type="text/css"> div {width:300px; height:200px; background-color:pink;} </style> </head> <body> <div></div> <input type="button" value="間接操作" onclick="f1()" /> </body> </html>