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