Prototype – 事件 observe() 方法

返回到: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>
   <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 – 事件处理

作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8549.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年1月31日
下一篇 2023年1月31日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注