JavaScript DHTML/Event onMethod/onClick
Содержание
Add document onclick event handler
<html>
<head>
<title>W3C DOM Event Propagation</title>
<script type="text/javascript">
function init() {
document.onclick = docEvent;
}
function docEvent(evt) {
alert("Document level .");
}
</script>
</head>
<body onload="init()">
</body>
</html>
Anchor onclick event
<html>
<head>
<title>Onclick</title>
<script type="text/javascript">
function handleclick() {
document.write("Clicked");
return false;
}
</script>
</head>
<body>
<p><a href="#" onclick="return handleclick();">Click Here</a></p>
</body>
</html>
"onClick" Example
<html>
<body>
<input type="text"
name="textfield"
onclick="alert("You clicked an input text field")"
value="Click me">
</body>
</html>
Put all your JavaScript statement in onclick clause
<html>
<head>
<title>A Simple Page</title>
</head>
<body>
<p onclick="units = prompt("value");alert(units + " is " + units * 9.99);">Click here to specify quantity</p>
</body>
</html>
Running a Script from User Action
<html>
<head>
<title>An onclick script</title>
<script type="text/javascript">
function alertUser() {
document.write("Hi!");
}
</script>
</head>
<body>
Here is some body text.
<form>
<input type="text" name="entry">
<input type="button" name="oneButton" value="Press Me!" onclick="alertUser()">
</form>
</body>
</html>