차근차근/JAVA Script

JavaScript Events

예쁜꽃이피었으면 2016. 2. 15. 15:27

http://www.w3schools.com/js/js_events.asp


JavaScript Events


HTML events are "things" that happen to HTML elements.

When JavaScript is used in HTML pages, JavaScript can "react" on these events.


HTML Events

An HTML event can be something the browser does, or something a user does.

Here are some examples of HTML events:

  • HTML 웹 페이지 로드 완료 시
  • HTML 입력 필드 변경 시
  • HTML 버튼을 클릭 시

Often, when events happen, you may want to do something.

JavaScript lets you execute code when events are detected.

HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.

With single quotes:

<some-HTML-element some-event='some JavaScript'>

With double quotes:

<some-HTML-element some-event="some JavaScript">

In the following example, an onclick attribute (with code), is added to a button element:

Example

<button onclick='getElementById("demo").innerHTML=Date()'>The time is?</button>
Try it Yourself »

In the example above, the JavaScript code changes the content of the element with id="demo".

In the next example, the code changes the content of its own element (using this.innerHTML):

Example

<button onclick="this.innerHTML=Date()">The time is?</button>
Try it Yourself »
NoteJavaScript code is often several lines long. It is more common to see event attributes calling functions:

Example

<button onclick="displayDate()">The time is?</button>
Try it Yourself »

Common HTML Events

Here is a list of some common HTML events:

EventDescription
onchangeAn HTML element has been changed
onclickThe user clicks an HTML element
onmouseoverThe user moves the mouse over an HTML element
onmouseoutThe user moves the mouse away from an HTML element
onkeydownThe user pushes a keyboard key
onloadThe browser has finished loading the page

The list is much longer: W3Schools JavaScript Reference HTML DOM Events.


What can JavaScript Do?

Event handlers can be used to handle, and verify, user input, user actions, and browser actions:

  • Things that should be done every time a page loads
  • Things that should be done when the page is closed
  • Action that should be performed when a user clicks a button
  • Content that should be verified when a user inputs data
  • And more ...

Many different methods can be used to let JavaScript work with events:

  • HTML event attributes can execute JavaScript code directly
  • HTML event attributes can call JavaScript functions
  • You can assign your own event handler functions to HTML elements
  • You can prevent events from being sent or being handled
  • And more ...
NoteYou will learn a lot more about events and event handlers in the HTML DOM chapters.

Test Yourself with Exercises!

Exercise 1 »   Exercise 2 »   Exercise 3 »





이렇게 했으면 됐는데.. 너무 어렵게 생각한듯..








반응형

'차근차근 > JAVA Script' 카테고리의 다른 글

네이버 지도 api v3 적용  (0) 2017.02.22
JavaScript Strings  (0) 2016.02.15
JavaScript Scope  (0) 2016.02.15
JavaScript Objects  (0) 2016.02.15
JavaScript Functions  (0) 2016.02.15