`
robinqu
  • 浏览: 88910 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Javascript 事件编程 (一)

阅读更多
Events and Event Handling
事件和事件处理

引用
The original event model: all js-enabled browsers
The standard event model: all modern explorer except IE
The Internet Explorer event model: IE

In the original event model, an event is an abstraction internal to the web browser, and JavaScript code cannot manipulate an event directly. When we speak of an event type in the original event model, what we really mean is the name of the event handler that is invoked in response to the event.

Semantic events, such as onsubmit and onchange, are almost always device-independent events: all modern browsers allow users to manipulate HTML forms using the mouse or keyboard traversal. The events that have the word "mouse" or "key" in them are clearly device-dependent events.
Note that the onclick event can be considered a device-independent event. It is not mouse-dependent because keyboard activation of form controls and hyperlinks also generates this event.


三类事件模型:
原始事件模型——所有启用JS的浏览器
标准模型——所有除IE以外的现代浏览器
IE事件模型——IE

在原始事件模型中,我们并不能操作事件对象本身。我们在这个模型中对事件名称的称呼多半是事件处理函数的名字。事件的内部情况完全被浏览器给抽象了。

事件又可以分为与设备依赖的事件和与设备非依赖的事件;具有语义性的事件,例如onsubmit、onchange等都是与设备无关的;而有一些例如onmouseover的事件则是与设备依赖的。
但onclick可以看做一个不依赖于设备的事件,因为键盘的点击也可以在超链接或表单元素上产生这个事件。


Event Handlers as Attributes
作为特性的事件句柄
<form action="processform.cgi" onsubmit="return validateForm( );">


Event Handlers as Properties
作为属性的事件句柄
document.f1.b1.onclick=function( ) { alert('Thanks!'); };


引用
Because the values of JavaScript event handler properties are functions, you can use JavaScript to invoke event handler functions directly.

Note, however, that invoking an event handler is not a way to simulate what happens when the event actually occurs.

by adding a handler for each hyperlink, it overwrites any onclick handlers that were already defined for those hyperlinks.


因为JS的事件处理函数是一个属性值,因此可以直接调用事件处理函数本身。
但这并不能模拟事件发生本身。

当你再次为某个超链接的onlcikc句柄添加时间处理函数,那么也就重载了之前指定的事件处理函数。

Event Handler Return Values
事件处理函数的返回值
引用

Generally, if the web browser performs some kind of default action in response to an event, you can return false to prevent the browser from performing that action.

There is one exception to the rule about returning false to cancel: when the user moves the mouse over a hyperlink, the browser's default action is to display the link's URL in the status line.

most modern browsers consider the ability to hide the destination of a link to be a security hole and have disabled it.


通常,如果浏览器对某个事件有默认多做,那么返回false就会取消那个默认多做。但也有一个例外,你不能在鼠标滑过一个超链接时通过返回false来取消提示信息的显示。因为大多数浏览器认为如果这样做了就是不安全的,所以就禁止了以下这种行为:

<a href="help.htm" onmouseover="window.status='Help!!'; return true;">Help</a>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics