| 情報学部 | 菅沼ホーム | 目次 | 索引 |
<INPUT ID="button1" TYPE="button" VALUE="ボタン" onMouseDown="func(event)"> (プログラム1の方法1)
<INPUT ID="button2" TYPE="button" VALUE="ボタン" onMouseDown="JavaScript: alert('イベントの種類: ' + event.type)"> (プログラム1の方法2)
プログラム1(下がそのソースコード)は,マウスが押されたときの処理の例である.イベント処理を,いくつかの方法で記述している.
01 <!DOCTYPE HTML>
02
03 <HTML>
04 <HEAD>
05 <TITLE>event</TITLE>
06 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
07 <META NAME=viewport CONTENT="width=device-width, initial-scale=1">
08 <LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css">
09 <SCRIPT TYPE="text/javascript">
10 function func(e)
11 {
12 let nw = open("", "new_page");
13 nw.document.open();
14 nw.document.writeln('<HTML>');
15 nw.document.writeln('<HEAD>');
16 nw.document.writeln(' <TITLE>イベント処理</TITLE>');
17 nw.document.writeln(' <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">');
18 nw.document.writeln(' <LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css">');
19 nw.document.writeln('</HEAD>');
20 nw.document.writeln('<BODY CLASS="white">');
21 nw.document.write(" イベントの種類: " + e.type + "<BR>");
22 nw.document.write(" 最初に送られたオブジェクト( target ): " + e.target + "<BR>");
23 nw.document.write(" マウスボタンの種類( which ): " + e.which + "<BR>");
24 nw.document.write(" マウスボタンの種類( button ): " + e.button + "<BR>");
25 nw.document.write(" キーコード( keyCode ): " + e.keyCode + "<BR>");
26 nw.document.write(" Shift キー( shiftKey ): " + e.shiftKey + "<BR>");
27 nw.document.write(" Ctrl キー( ctrlKey ): " + e.ctrlKey + "<BR>");
28 nw.document.write(" Alt キー( altKey ): " + e.altKey + "<BR>");
29 nw.document.write(" ディスプレイ上の X 軸の値( screenX ): " + e.screenX + "<BR>");
30 nw.document.write(" ディスプレイ上の Y 軸の値( screenY ): " + e.screenY + "<BR>");
31 nw.document.write(" イベントが発生した要素上の X 軸の値( offsetX ): " + e.offsetX + "<BR>");
32 nw.document.write(" イベントが発生した要素上の Y 軸の値( offsetY ): " + e.offsetY + "<BR>");
33 nw.document.write(" Window 上の X 座標( clientX ): " + e.clientX + "<BR>");
34 nw.document.write(" Window 上の Y 座標( clientY ): " + e.clientY + "<BR>");
35 nw.document.write(" ページ上の X 座標( pageX ): " + e.pageX + "<BR>");
36 nw.document.write(" ページ上の Y 座標( pageY ): " + e.pageY + "<BR>");
37 nw.document.write(" ページ上の X 座標( x ): " + e.x + "<BR>");
38 nw.document.write(" ページ上の Y 座標( y ): " + e.y + "<BR>");
39 nw.document.writeln('</BODY>');
40 nw.document.writeln('</HTML>');
41 nw.document.close();
42 }
43
44 function func3_2(e)
45 {
46 func(e);
47 }
48 </SCRIPT>
49 </HEAD>
50 <BODY CLASS="white">
51 <H1 STYLE="text-align: center">マウスイベント</H1>
52 <FORM><P STYLE="text-align: center">
53 <INPUT ID="button1" TYPE="button" VALUE="方法1" onMouseDown="func(event)">
54 <INPUT ID="button2" TYPE="button" VALUE="方法2" onMouseDown="JavaScript: alert('event.type : ' + event.type);">
55 <INPUT ID="button3_1" TYPE="button" VALUE="方法3-1">
56 <INPUT ID="button3_2" TYPE="button" VALUE="方法3-2">
57 <INPUT ID="button3_3" TYPE="button" VALUE="方法3-3">
58 </P></FORM>
59 <SCRIPT TYPE="text/javascript">
60 // ロード時にイベントリスナを追加
61 // onMouseDown 属性に関数を直接設定
62 document.getElementById("button3_1").onmousedown = function(e) {
63 func(e);
64 };
65 // 関数を外部に記述
66 document.getElementById("button3_2").addEventListener("mousedown", func3_2, false);
67 // 関数を以下のように内部に記述
68 document.getElementById("button3_3").addEventListener("mousedown", function(e) {
69 func(e);
70 }, false);
71 </SCRIPT>
72 </BODY>
72 </HTML>
プログラム2(下がそのソースコード)は,キーが押されたときの処理の例である( BODY 要素において onKeyDown 属性を指定).この例のように,TABINDEX 属性の指定も行っておくべきである.
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>event</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<META NAME=viewport CONTENT="width=device-width, initial-scale=1">
<LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css">
<SCRIPT TYPE="text/javascript">
function func(e)
{
let nw = open("", "new_page");
nw.document.open();
nw.document.writeln('<HTML>');
nw.document.writeln('<HEAD>');
nw.document.writeln(' <TITLE>イベント処理</TITLE>');
nw.document.writeln(' <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">');
nw.document.writeln(' <LINK REL="stylesheet" TYPE="text/css" HREF="../../../master.css">');
nw.document.writeln('</HEAD>');
nw.document.writeln('<BODY CLASS="white">');
nw.document.write(" イベントの種類: " + e.type + "<BR>");
nw.document.write(" 最初に送られたオブジェクト( target ): " + e.target + "<BR>");
nw.document.write(" マウスボタンの種類( which ): " + e.which + "<BR>");
nw.document.write(" マウスボタンの種類( button ): " + e.button + "<BR>");
nw.document.write(" キーコード( keyCode ): " + e.keyCode + "<BR>");
nw.document.write(" Shift キー( shiftKey ): " + e.shiftKey + "<BR>");
nw.document.write(" Ctrl キー( ctrlKey ): " + e.ctrlKey + "<BR>");
nw.document.write(" Alt キー( altKey ): " + e.altKey + "<BR>");
nw.document.write(" ディスプレイ上の X 軸の値( screenX ): " + e.screenX + "<BR>");
nw.document.write(" ディスプレイ上の Y 軸の値( screenY ): " + e.screenY + "<BR>");
nw.document.write(" イベントが発生した要素上の X 軸の値( offsetX ): " + e.offsetX + "<BR>");
nw.document.write(" イベントが発生した要素上の Y 軸の値( offsetY ): " + e.offsetY + "<BR>");
nw.document.write(" Window 上の X 座標( clientX ): " + e.clientX + "<BR>");
nw.document.write(" Window 上の Y 座標( clientY ): " + e.clientY + "<BR>");
nw.document.write(" ページ上の X 座標( pageX ): " + e.pageX + "<BR>");
nw.document.write(" ページ上の Y 座標( pageY ): " + e.pageY + "<BR>");
nw.document.write(" ページ上の X 座標( x ): " + e.x + "<BR>");
nw.document.write(" ページ上の Y 座標( y ): " + e.y + "<BR>");
nw.document.writeln('</BODY>');
nw.document.writeln('</HTML>');
nw.document.close();
}
</SCRIPT>
</HEAD>
<BODY CLASS="white" onKeyDown="func(event)" TABINDEX="1">
<H1 STYLE="text-align: center">キーイベント</H1>
<H3 STYLE="text-align: center">キーを押してください</H3>
</BODY>
</HTML>
| 情報学部 | 菅沼ホーム | 目次 | 索引 |