上一篇介紹了Javascript初學者實例教程(1):簡單交互
實例二、
本實例主要介紹了event對象和事件的簡單應用。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>mouse</title>
<script language="javascript">
function catchEvent()
{
var eventSrcID = event.srcElement.id;
var eventtype = event.type;
alert(eventSrcID+"捕獲到了"+eventtype+"事件");
}
function GetPosition()
{
var posX = event.clientX;
var posY = event.clientY;
window.status = "鼠標的位置是("+posX+","+posY+")";
}
function GetKey()
{
textfield.value=event.keyCode+","+String.fromCharCode(event.keyCode);
}
</script>
</head>
<body onMouseMove="GetPosition();" >
鼠標在文本框中按下:<input type="text" name="textfield" onMouseDown="alert('鼠標在文本框中按下')"><br/>
鍵盤按下:<input type="text" name="textfield" onKeyDown="alert('鍵盤按下');"><br/>
event對象:<br/>
<input type="text" name="textfield" id="text" onClick="catchEvent();"><br/>
<input type="submit" name="Submit" value="提交" id="button" onClick="catchEvent();"><br/>
</body>
</html>
效果演示:
[Ctrl+A 全部選擇 提示:你