Microsoft AJAX Library Cheat Sheet(4):DomEvent类

本文为翻译,英文原版的Cheat Sheet(PDF版本)在此下载:http://aspnetresources.com/downloads/ms_ajax_library_cheat_sheets1.zip

原作版权声明:

Copyright (c) 2004-2006, Milan Negovan 
http://www.AspNetResources.com
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:

    * Redistributions of source code must retain the above copyright 
      notice, this list of conditions and the following disclaimer.
      
    * Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in 
      the documentation and/or other materials provided with the 
      distribution.
      
    * The name of the author may not be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 

注:标注有[S]的为静态方法,无须实例化对象即可使用。

 

[S] addHandler (element, eventName, handler)
[S] $addHandler (element, eventName, handler)

为DOM元素添加某一事件的处理函数,事件名称应去掉“on”前缀。

注意:在事件处理函数中,this指向的是element,而不一定是发出该事件的元素。

Sys.UI.DomEvent.addHandler (element, "click", clickHandler);
// Same as $addHandler (element, "click", clickHandler);
function clickHandler (e) { … }

 

[S] addHandlers (element, events, handlerOwner)
[S] $addHandlers (element, events, handlerOwner)

为DOM元素添加多个事件处理函数,events是一个事件处理函数的字典。

注意:在事件处理函数中,若指定了handlerOwner,那么this将指向该handlerOwner,否则将指向element。

$addHandlers ($get ("article"), {
    mouseover: onMouseOver,
    mouseout: onMouseOut
});
function onMouseOver (e) { this.style.backgroundColor = 'yellow'; }
function onMouseOut (e) { this.style.backgroundColor = 'white'; }

 

[S] clearHandlers (element)
[S] $clearHandlers (element)

移除指定DOM元素所有的事件处理函数。

Sys.UI.DomEvent.clearHandlers (element);
// Same as $ clearHandlers (element);

 

[S] removeHandler (element, eventName, handler)
[S] $removeHandler (element, eventName, handler)

为DOM元素移除指定的事件处理函数。

Sys.UI.DomEvent.removeHandler (element, "click", clickHandler);
// Same as $removeHandler (element, "click", clickHandler);

 

preventDefault ()

阻止执行默认的事件处理函数。例如若阻止了a元素的onclick事件,那么将不会引发页面导航。

$addHandler ($get ("showMoreLink"), "click", showMore);
function showMore (e) { e.preventDefault (); }

 

stopPropagation ()

阻止事件冒泡传递至父元素。

 

Event对象的属性

  1. altKey:判断触发事件时Alt键是否被按下。
  2. button:得到触发事件的鼠标按键。可选值为Sys.UI.MouseButton枚举(leftButton、middleButton和rightButton)。
  3. charCode:得到触发事件时的键盘按键代码。可以为Sys.UI.Key枚举(backspace, tab, enter,esc, space, pageUp, pageDown, end, home, left, up, right, down, del)。
  4. clientX:鼠标指针相对于文档可见区域的X坐标。
  5. clientY:鼠标指针相对于文档可见区域的Y坐标。
  6. ctrlKey:判断触发事件时Ctrl键是否被按下。
  7. offsetX:鼠标指针相对于触发事件元素左侧边缘的偏移位置。
  8. offsetY:鼠标指针相对于触发事件元素上边缘的偏移位置。
  9. rawEvent:原始的DOM事件。
  10. screenX:鼠标指针相对于浏览者屏幕的X坐标。
  11. screenY:鼠标指针相对于浏览者屏幕的Y坐标。
  12. shiftKey:判断触发事件时Shift键是否被按下。
  13. target:触发事件的对象。
  14. type:事件名称(例如“click”)。
posted on 2007-02-07 22:13  Dflying Chen  阅读(2913)  评论(3编辑  收藏  举报