JavaScript Tutorial/Event/Event Parameter
Содержание
Get event Phase: CAPTURING,BUBBLING
<source lang="javascript">
<html> <head> <title>W3C DOM Event Propagation</title> <script type="text/javascript">
function getPhase(evt) {
switch (evt.eventPhase) { case 1: return "CAPTURING"; break; case 2: return "AT TARGET"; break; case 3: return "BUBBLING"; break; default: return ""; }
} </script> </head> <body onload="init()"> <form> <input type="button" value="Button "main1"" name="main1" onclick="alert("button (" + getPhase(event) + ").")" /> </form> </body> </html></source>
Pass key event as the function parameter
<source lang="javascript">
<html>
<head> <title>Key Events Example</title> <script type="text/javascript"> function handleEvent(oEvent) { var oTextbox = document.getElementById("txt1"); oTextbox.value += "\n>" + oEvent.type; } </script> </head> <body>
Type some characters into the first textbox.
<textarea id="txtInput" rows="15" cols="50" onkeydown="handleEvent(event)" onkeyup="handleEvent(event)" onkeypress="handleEvent(event)"></textarea>
<textarea id="txt1" rows="15" cols="50"></textarea>
</body>
</html></source>
Use mouse event as the function parameter
<source lang="javascript">
<html> <head> <title>Mouse Events Example</title> <script type="text/javascript">
function handleEvent(oEvent) { var oTextbox = document.getElementById("txt1"); oTextbox.value += "\n" + oEvent.type;
}
</script> </head> <body>
Use your mouse to click and double click the red square.
<textarea id="txt1" rows="15" cols="50"></textarea>
</body> </html></source>
X/Y Marks the Spot for a popup window (IE)
<source lang="javascript">
<html> <head> <title>X/Y Marks the Spot</title> <script type="text/javascript"> function mouseDown() {
var locString = "X = " + window.event.screenX + " Y = " + window.event.screenY; document.write(locString);
} document.onmousedown=mouseDown; </script> </head> <body> </body> </html></source>