<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>onclick event example</title>
<script>
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(event) {
alert("onclick Event detected!");
}
</script>
<style>
#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>
更多建議: