- 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 - 討論
原型 - 事件 findElement() 方法
此方法返回第一個具有給定標籤名的 DOM 元素,該標籤向上位於發生事件的元素上。
有時,你並不關心實際被事件點選的元素。有時你對它的“最近元素”更感興趣。這就是 findElement 的用途。
所提供的標籤名稱將以不區分大小寫的方式進行比較。
語法
Event.findElement(event, tagName);
返回值
返回第一個具有給定標籤名的 DOM 元素。如果沒有找到匹配元素,則返回文件本身(HTMLDocument 節點)。
示例
以下是一個簡單的程式碼,它允許你單擊頁面上的任意位置並隱藏圍繞你點選位置最貼近的段落(如果有)。
<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) {
var element = Event.findElement(event, 'P');
alert("Hiding Tag : " + element.tagName );
if ( element != document ) {
element.hide();
}
}
</script>
</head>
<body>
<p id = "note"> Click anywhere to see the result.</p>
<p id = "para1">This is paragraph 1</p>
<br />
<br />
<p id = "para2">This is paragraph 2</p>
<div id = "division">This is divsion.</div>
</body>
</html>
輸出
prototype_event_handling.htm
廣告