jQueryのイベントシステムは、W3C標準に準拠した実装になっています。イベントオブジェクトはjQueryのイベントハンドラーに確実に渡されます。(コールバック関数に引数として渡されず、window.eventなどから取得するタイプのブラウザであっても、そのようなチェックは必要ありません)
オリジナルのイベントオブジェクトが持つ殆どのプロパティは、このオブジェクトにコピーされます。
var e = jQuery.Event("click");Example:
var e = new jQuery.Event("click");
$("p").click(function(event){ alert(event.type); });
[click]
$("a[href=http://www.homes.co.jp]").click(function(event){ alert(event.target.href); });[HOME'S]
<ul> <li>ここは全てli要素</li> <li>この要素の<b>ここ</b>はb要素</li> <li><span>ここは全てliではなく、span要素</span></li> <ul>
function sampleTargetHandler(event){ var target = $(event.target); if(target.is("li")){ alert(target.html()); } } $("ul").click(sampleTargetHandler);
$("div").mouseout(function(event){ alert(event.relatedTarget); });
<p>ここをクリックしても、太字部分を<b>クリック<b>しても、結果は常に「<b>p<b>」<p>
$("p").click(function(event){ alert(event.currentTarget.nodeName); });
[ここをクリックしても、太字部分をクリックしても、結果は常に「p」]
$("a").click(function(event){ alert(event.pageX + ":" + event.pageY); });
[click]
$("p").click(function(event){ return "hey"; }); $("p").click(function(event){ alert(event.result); });
[click]
var last; $("p").click(function(event){ if(last) alert(event.timeStamp - last); last = event.timeStamp; });最初の1クリックは、何も表示されません。
[click]
$("a").click(function(event){ alert(this.href); event.preventDefault(); });[jQuery official]
$("a").click(function(event){ alert('1: '+event.isDefaultPrevented()); event.preventDefault(); }); $("a").click(function(event){ alert('2: '+event.isDefaultPrevented()); });[jQuery official]
<div id="sample-stoppropagation"><span id="sample-stoppropagation-in">[click]</span></div>
$("span").click(function(event){ alert(this.tagName); event.stopPropagation(); }); $("div").click(function(event){ alert(this.tagName); });
$("p").click(function(event){ alert(event.isPropagationStopped()); event.stopPropagation(); alert(event.isPropagationStopped()); });
[click]
$("p").click(function(event){ alert("event 1"); event.stopImmediatePropagation(); }); $("p").click(function(event){ alert("event 2"); });
[click]
$("p").click(function(event){ alert(event.isImmediatePropagationStopped() + ', ' + event.isPropagationStopped()); event.stopImmediatePropagation(); alert(event.isImmediatePropagationStopped() + ', ' + event.isPropagationStopped()); });
[click]