- Prototype 教程
- Prototype - 主页
- Prototype - 简要概述
- Prototype - 有用功能
- Prototype - 实用方法
- Prototype - 元素对象
- Prototype - 数字处理
- Prototype - 字符串处理
- Prototype - 数组处理
- Prototype - 哈希处理
- Prototype - 基本对象
- Prototype - 模板化
- Prototype - 枚举
- Prototype - 事件处理
- Prototype - 表单管理
- Prototype - JSON 支持
- Prototype - AJAX支持
- Prototype - 表达范围
- Prototype - 定期执行
- Prototype 实用资源
- Prototype - 快速指南
- Prototype - 实用资源
- Prototype - 讨论
原型 - 事件观察 () 方法
此方法在 DOM 元素上注册一个事件处理程序。
要将一个函数注册为事件处理程序,您想观察的 DOM 元素必须已经存在于 DOM 中。
语法
Event.observe(element,eventName,handler[,useCapture=false]);
以下是有关传递的参数的说明 -
element - 您希望观察的 DOM 元素;与 Prototype 中一样,这可以是实际的 DOM 引用,也可以是该元素的 ID 字符串。
eventName - 标准化的事件名称,根据浏览器支持的 DOM 级别而定。这包括单击、鼠标按下、鼠标松开、鼠标移入、鼠标移动和鼠标移出。
handler - 这是事件处理程序函数。这可以是您随时创建的匿名函数。
useCapture - 或者,您可以请求捕获而不是冒泡。详情请见 http://www.w3.org/TR/DOM-Level-2Events/events.html。
返回值
不适用。
示例
下面是一个示例,它观察单击事件,并在发生单击事件时采取措施。
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
// Register event 'click' and associated call back.
Event.observe(document, 'click', respondToClick);
// Callback function to handle the event.
function respondToClick(event) {
alert("You pressed the button...." );
}
</script>
</head>
<body>
<p id = "note"> Click anywhere to see the result.</p>
<p id = "para1">This is paragraph 1</p>
<p id = "para2">This is paragraph 2</p>
<div id = "division">This is divsion.</div>
</body>
</html>
输出
prototype_event_handling.htm
广告