简体   繁体   English

浏览器如何处理内联javascript事件?

[英]How does browser handle inline javascript events?

I need to pass event object and event origin object to handling function. 我需要将事件对象和事件源对象传递给处理函数。 I believe I understand how external javascript works: 我相信我理解外部JavaScript是如何工作的:

<input id="input2"/>
<script>
function getKey2(e) { alert(this.id+e.keyCode); }
document.getElementById("input2").onkeypress = getKey2;
</script>

the onkeypress method is defined on input2 DOM, so this points to it and event object is passed as the first argument to the function. 该onkeypress事件方法被限定在输入2 DOM,因此指向它和事件对象作为第一个参数传递给函数。 But I am confused with inline javascript : 但我对内联javascript感到困惑:

<input id="input1" onkeypress="getKey1(this,event)"/>
<script>
function getKey1(obj,e) {
    alert(obj.id+e.keyCode);
}
</script>

The first argument, this , should be Window object, but when it is copied to obj on the event, it is event origin object, the input1 from DOM. 第一个参数, 这一点 ,应该是Window对象,但是当它被复制到obj上的事件,它是事件源对象,从DOM中输入1。 The second argument, event , should be global event object, but when called, event object is passed to the function. 第二个参数event应该是全局事件对象,但是在调用时,会将事件对象传递给函数。 Apparently, my deduction is wrong - how does the call work? 显然,我的推论是错误的 - 通话如何运作?

Long story short: why the object values in the following chars are not the same? 长话短说:为什么以下字符中的对象值不一样?

              | name   object value
----------------------------------
in onkeypress | this   Window
in getKey1    | obj    DOM input1


              | name   object value
----------------------------------
in onkeypress | event  global event
in getKey1    | e      keypress event

fiddle here 在这里小提琴

The W3C DOM specification states two bindings for inline events: W3C DOM规范规定了内联事件的两个绑定:

  1. this is bound to the element upon which the inline event was defined ; this定义内联事件的元素绑定;
  2. A local variable with the name event is introduced. 引入了名为event局部变量。 (IE doesn't do this but the same syntax works for inline event because event will be treated as window.event in IE.) (IE不会这样做,但相同的语法适用于内联事件,因为在IE中event将被视为window.event 。)

In HTML5 the information is buried under 6.1.6.1 Event handlers . 在HTML5中,信息隐藏在6.1.6.1事件处理程序下 The two parts are spread out: 这两部分分散开来:

When an event handler content attribute is set [that is, when an inline event is set] .. Let body [of the synthesized function declaration] be the event handler content attribute's new value .. Let the function have a single argument called event . 当设置事件处理程序内容属性时[即,当设置内联事件时] ..让合成函数声明的body []为事件处理程序内容属性的新值。 让函数有一个名为event的参数 [Note that onerror is a special.] [注意, onerror是特殊的。]

.. Let Scope [or this ] be the result of NewObjectEnvironment(the element's object, Scope). .. 让Scope [或this ]成为NewObjectEnvironment(元素的对象,Scope)的结果。 [This is chained with other environment contexts such as the Form (if it exists) and the Global Context.] [这与其他环境上下文链接,例如表单(如果存在)和全局上下文。]

no, this will be the dom object on which the event was triggered, in this case the <input> were the onkeyup attribute was set and where the browser will find a handler. 不,这将是触发事件的dom对象,在这种情况下, <input>是设置的onkeyup属性以及浏览器将在哪里找到处理程序。

We are going to play a little more with those events: http://jsfiddle.net/nFfEp/ 我们将在这些活动中发挥更多作用: http//jsfiddle.net/nFfEp/

For better understanding about the event triggering and handling this is a very useful document: http://www.w3.org/TR/DOM-Level-3-Events/#event-flow 为了更好地理解事件触发和处理,这是一个非常有用的文档: http//www.w3.org/TR/DOM-Level-3-Events/#event-flow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM