イベント(Event)

[up] [prev] [next]

このページの内容は Internet Explorer や Firefox の前身である Netscape Navigator の時代の古いものです。

目次

イベントオブジェクト(Internet Explorer)

window.event

onClick="..." などのイベントハンドラの中で参照できるイベントオブジェクトです。イベントの概要については「イベントハンドラ」を参照してください。

window.event.type

イベントタイプ。たとえば、onClick の時は "click" という文字列となります。

HTML
<input type="button" value="Click Me!!"
  onclick="alert(window.event.type)">
window.event.keyCode
window.event.shiftKey
window.event.ctrlKey
window.event.altKey
window.event.button

keyCode は、イベントが発生した時に、キーボード上のどのキーが押されていたかを示す値(ASCIIコード)。shiftKey、ctrlKey、altKey は、イベントが発生した時に、Shiftキー、Ctrlキー、Altキーが押されていたかどうかを示す真偽値。button は、イベントが発生した時に、マウスのどのボタンが押されていたかを示す値。1 は左ボタン、2 は右ボタン、4 は真中ボタンを示し、2 つ以上のボタンが押された場合はその合計値となります。

HTML
<input type="text" onkeypress="alert(window.event.keyCode)">
<input type="button" value="Click Me!!"
  onclick="alert(window.event.button)">
window.event.screenX
window.event.screenY
window.event.clientX
window.event.clientY
window.event.x
window.event.y
window.event.offsetX
window.event.offsetY

このイベントが発生した場所の座標。screenX、screenY はスクリーン上の座標、clientX、clientY はクライアント領域(ウィンドウ)上の座標、x、y は配置された親要素(通常は BODY 要素)上の座標、offsetX、offsetY は、クリックした要素上の座標を示します。

HTML
<img src="xxx.gif" alt="xxx" onclick="alert(
  'screen=' + event.screenX + ',' + event.screenY +
  ', client=' + event.clientX + ',' + event.clientY +
  ', xy=' + event.x + ',' + event.y +
  ', offset=' + event.offsetX + ',' + event.offsetY)">
window.event.srcElement
window.event.fromElement

srcElement は、このイベントが発生した要素を示すオブジェクト。fromElement は、onMouseOver および onMouseOut イベントが発生した際の、マウスの移動元の要素を示すオブジェクト。

HTML
<img src="xxx.gif" alt="xxx" onclick="alert(event.srcElement.src)">
window.event.srcFilter

onFilterChange イベントが発生した際の、発生元フィルタオブジェクト。

window.event.cancelBubble

このイベントを上位の要素のイベントハンドラに渡すかどうかを示す真偽値。true を代入すると、上位のイベントハンドラが呼ばれなくなります。

HTML
<script>
function child() {
  alert("child");
  event.cancelBubble = true;
}
function parent() {
  alert("parent");
}
</script>
<div style="background-color:red; width:200; height:200;"
   onclick="parent()">
 <div style="background-color:blue; width:100; height:100;"
    onclick="child()">
 </div>
</div>
window.event.returnValue = value

このイベントハンドラの戻り値。通常は「return value;」とするのと同等です。false を代入することにより、「Submitボタンを押した」や「リンクをクリックした」などの、イベント本来の動作を抑制することができます。

window.event.reason

データソースオブジェクトにおけるデータ転送の状態。0 は成功、1 は中断、2 は失敗を示します。

イベントオブジェクト(Netscape Communicator)

object.captureEvents(eventName)

Netscape Communicator 4.0 でサポートされたイベント処理技術で、イベントのキャプチャを開始します。eventName には、Event.イベント名 を指定します。

event

イベントハンドラの中で参照可能な event オブジェクトです。

HTML
<script>
function func(e) {
  alert(e.height + "," + e.width);
}
</script>
<form action="#">
<input type="button" name="B1" onclick="func(event)" value="OK">
</form>

「onxxxx = 関数名」でキャプチャしたイベントハンドラには引数として渡されます。

HTML
<script>
window.captureEvents(Event.CLICK);
window.onclick = func;
function func(e) {
  alert(e.height + "," + e.width);
}
</script>
event.target

イメージオブジェクトやボタンオブジェクトなど、最初にイベントが送られたオブジェクトを示します。

event.type

発生したイベントのタイプを "click" などの文字列で表します。

event.data

DragDrop の際、ドラッグ&ドロップしたオブジェクト(ファイルなど)の URL を示す文字列の配列。

event.which

どのマウスボタンが押されたかを示します。1 が左ボタン、2 が(あれば)真ん中ボタン、3 が右ボタンとなります。

JavaScript
document.onmousedown = func;
function func(e) { alert(e.which); }
event.modifiers

イベント発生時にどの特殊キーが押されていたかを示します。Windows の場合は Alt(1)、Ctrl(2)、Shift(4)、Meta(8)の、Macintosh の場合はOption(1)、Ctrl(2)、Shift(4)、Command(8)の合計が値となります。

event.screenX
event.screenY
event.pageX
event.pageY
event.width
event.height
event.layerX
event.layerY

イベントが発生した場所の、screenX、screenY はスクリーン(ディスプレイ)上の座標、pageX、pageY はページ上の座標、width、height はウィンドウ(フレーム)上の座標、layerX、layerY はレイヤー上の座標、を示します。


[up] [prev] [next]
Copyright (C) 1996-2001 杜甫々
改訂版初版:2001年5月20日
http://www.tohoho-web.com/js/event.htm