Web覚書トップ > JavaScript サンプル > 『画面上のクリックで背景色を変えてみる』
説明

『画面上のクリックを取得してみる』のアレンジ版です。

実行例

画面上で右クリックや左クリックをしてみてください

コード
function setEventsCatch(){
	if (document.all || document.getElementById) {
		document.onmousedown=mouseDown;
	} else if (document.layers) {
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown=mouseDown;
	}
}
function mouseDown(e){
	if (document.all) {
		if (event.button == 1) {
			window.document.bgColor = "#ccccff";
		}else if (event.button == 2 || event.button == 3){
			window.document.bgColor = "#ffcccc";
		}
	} else {
		if (e.which == 1) {
			window.document.bgColor = "#ccccff";
		} else if (e.which == 3) {
			window.document.bgColor = "#ffcccc";
		}
	}
}

解説&ステップアップ

右クリックのときにはメニューが出てしまいます。

動作確認

IE4.0(win)、IE5.0(win)、IE5.5(win)、NN4.7(win)

© hyork@yahoo.co.jp (2001. 3.15)