5.3 Event 扩展

优质
小牛编辑
130浏览
2023-12-01
PropertyTypeDescription
KEY_BACKSPACENumberNumber8: Constant. Code for the Backspace key.
KEY_TABNumber9: Constant. Code for the Tab key.
KEY_RETURNNumber13: Constant. Code for the Return key.
KEY_ESCNumber27: Constant. Code for the Esc key.
KEY_LEFTNumber37: Constant. Code for the Left arrow key.
KEY_UPNumber38: Constant. Code for the Up arrow key.
KEY_RIGHTNumber39: Constant. Code for the Right arrow key.
KEY_DOWNNumber40: Constant. Code for the Down arrow key.
KEY_DELETENumber46: Constant. Code for the Delete key.
observers:ArrayList of cached observers. Part of the internal implementation details of the object.
MethodKindArgumentsDescription
element(event)staticevent: an Event object返回事件源对象。
isLeftClick(event)staticevent: an Event object如果点击了鼠标左键,返回true.
pointerX(event)staticevent: an Event object返回鼠标的X座标。
pointerY(event)staticevent: an Event object返回鼠标的Y座标。
stop(event)staticevent: an Event object使用此函数来中断事件的默认行为并阻止传递(冒泡)。
findElement(event, tagName)staticevent: an Event object, tagName: name of the desired tag.从事件源对象开始向上搜索DOM树,直到找到第一个符合tagName的元素
observe(element, name, observer, useCapture)staticelement: object or id, name: event name (like 'click', 'load', etc), observer: function to handle the event, useCapture: if true, handles the event in the capture phase and if false in the bubbling phase.为对象的某个事件增加一个处理函数。
stopObserving(element, name, observer, useCapture)staticelement: object or id, name: event name (like 'click'), observer: function that is handling the event, useCapture: if true handles the event in the capture phase and if false in the bubbling phase.和上面的函数相反。
_observeAndCache(element, name, observer, useCapture)static私有函数,别管它。
unloadCache()static(none)私有函数,别管它。从内存中清除所有的observers缓存。

下面代码演示如何给window添加一个load事件处理函数。

<script>
Event.observe(window, 'load', showMessage, false);
function showMessage() {
alert('Page loaded.');
}
</script>