https://developer.mozilla.org/en/DOM/element.onclick
概要
onclick プロパティは、現在の要素の onClick イベント ハンドラー コードを返します。
構文
element.onclick = functionRef;
ここで、functionRef は関数です。多くの場合、別の場所で宣言された関数の名前または関数式です。詳細については、コア JavaScript 1.5 リファレンス:関数を参照してください。
例
<!doctype html>
<html>
<head>
<title>onclick event example</title>
<script type="text/javascript">
function initElement()
{
var p = document.getElementById("foo");
// NOTE: showAlert(); or showAlert(param); will NOT work here.
// Must be a reference to a function name, not a function call.
p.onclick = showAlert;
};
function showAlert()
{
alert("onclick Event detected!")
}
</script>
<style type="text/css">
#foo {
border: solid blue 2px;
}
</style>
</head>
<body onload="initElement()";>
<span id="foo">My Event Element</span>
<p>click on the above element.</p>
</body>
</html>
または、次のように無名関数を使用できます。
p.onclick = function() { alert("moot!"); };
( MDC @cc-by-sa
より)