Is 'event' a reserved word in JavaScript?

前端 未结 6 1868
谎友^
谎友^ 2020-12-09 01:47

I am a beginner to Javascript. And when I was practicing I have noticed something.

Take this function:



        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 02:32

    No, event is not a reserved word. It is, however, a variable which is set by all the major browsers when an event handler (such as onkeypress) of a DOM node is being executed. In IE, it is also a global variable.

    A typical cross-browser way to get the event is along these lines.

    On the DOM node:

    Click me

    The event handling function:

    function someFunction(evt) {
      var srcElem = evt.srcElement || evt.target;
    
      // Code continues
    }
    

    By the way, in your example, oEvent is the name of the parameter and is therefore valid in the context of the function being called, not in the context of the caller.

提交回复
热议问题