JavaWeb-JS events


1. Brief description of JS events

Events , such as: click, form submission, etc.;
through JS events, complete the specified special effects of the page;
Insert picture description here
Insert picture description here

Two, JS event-driven mechanism

JS event-driven mechanism :

  • Event source ;
    components that specifically generate events;
  • Event ;
    an action or thing generated by an event source;
  • Listener ;
    specializes in processing events generated by the event source;
  • Register/bind the listener ;
    let the listener monitor the event source at any time whether a specified event occurs, if the event source generates a specified event, call the listener to process;

Three, common JS events

1. Click event (onclick)

  • Click event: triggered when the mouse or hotkey clicks on the element component;
    Insert picture description here

2. Focus event

  • Get focus event (onfocus)
    focus: the attention of the entire interface; a page has only one focus
    Insert picture description here

  • Loss of focus event (onblur)
    Insert picture description here

3. Domain content change event (onchange)

  • Triggered when the value of the element component changes;
    Insert picture description here

4. Load finished event (onload)

  • Triggered when the element component is loaded;
    Insert picture description here

5. Form submission event (onsubmit)

  • Triggered when the submit button of the form is clicked ;
  • Mainly used for form verification;
  • This event can also control the submission of the form; this event needs to return a boolean value to perform the operation of submitting/blocking the form data;
    true means allowing form submission; false means organizing form submission;
    Insert picture description here

6. The key up event (onkeyup)

  • When inputting some content in the component, the event is triggered when the keyboard key is up;
    Insert picture description here

7. Common mouse events

  • Mouse over event (onmouseover)
    is triggered when the mouse moves into an element component;
    Insert picture description here
  • Mouse out event (onmouseout)
    Insert picture description here

Four, JS event binding method

1. Element event handler binding

  • Write the event into the tag as an element attribute, and then bind the corresponding function;
    Insert picture description here
    Insert picture description here
  • Bind multiple events (the binding order is the execution order)
    Insert picture description here

  • Advantages and disadvantages of event handle binding : Advantages: fast development; convenient parameter transfer; multiple functions can be bound;
    disadvantages: JS and HTML codes are highly soft together, which is not conducive to multi-departmental project development and maintenance;

2. DOM binding method (the most commonly used method at present)

Insert picture description here

Insert picture description here
Insert picture description here

  • The advantages and disadvantages of the DOM binding method :
    advantages: completely separate HTML code and JS code;
    disadvantages: no parameters can be passed; only one function can be bound to an event; (both disadvantages can be solved by anonymous internal functions)

Guess you like

Origin blog.csdn.net/pary__for/article/details/111033284