JavaScript events (basic)

First, the event

  Event: Trigger - Response Mechanism.

Second, the three elements of the event

  1 Event Source : Trigger element event

  2, event name : send the event in what way the

  3, event handlers : event-triggered code to be executed (functional form)

Third, the use of events

  1, to obtain elements

  2, to register an event element

  3, the preparation of the event content

  Demo:

. 1  var box = document.getElementById ( 'box' );
 2 box.onclick = function () {// anonymous function
 . 3    the console.log ( 'code will be executed after the box is clicked' );  
 4 };

  Note : You can directly to the event when a triggering event of anonymous functions, can also declare a function, and then assigned to the event.

  Demo:

. 1  var Box = document.getElementById ( 'Box' );
 2 box.onclick = box_click; // this function can not be added later in parentheses (), it would put parentheses function call, the system call functions to complete , assigned to the event here is
 3  // declare function 
. 4  function btn_Click () {
 . 5      Alert ( 'Hello JavaScript' );
 . 6  }
 . 7  

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11406910.html