返回到:Prototype – 事件处理
此方法在 DOM 元素上注册事件处理程序。
要将函数注册为事件处理程序,您要观察的 DOM 元素必须已经存在于 DOM 中。
语法
Event.observe(element,eventName,handler[,useCapture=false]);
以下是有关传递参数的解释 –
- element – 你想要观察的 DOM 元素;与在 Prototype 中一样,这可以是实际的 DOM 引用,也可以是元素的 ID 字符串。
- evenetName – 标准化的事件名称,根据浏览器支持的 DOM 级别。这包括 click、mousedown、mouseup、mouseover、mousemove 和 mouseout。
- handler — 这是事件处理函数。这可以是您即时创建的匿名函数。
- useCapture – 可选地,您可以请求捕获而不是冒泡。详细信息在http://www.w3.org/TR/DOM-Level-2Events/events.html中。
返回值
NA.
例子
这是一个示例,它观察点击事件并在点击事件发生时采取行动。
HTML
x
23
23
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
// Register event 'click' and associated call back.
8
Event.observe(document, 'click', respondToClick);
9
10
// Callback function to handle the event.
11
function respondToClick(event) {
12
alert("You pressed the button...." );
13
}
14
</script>
15
</head>
16
17
<body>
18
<p id = "note"> Click anywhere to see the result.</p>
19
<p id = "para1">This is paragraph 1</p>
20
<p id = "para2">This is paragraph 2</p>
21
<div id = "division">This is divsion.</div>
22
</body>
23
</html>
返回到:Prototype – 事件处理
阅读剩余 44%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8549.html