JavaScript even...LOGIN

JavaScript events

JavaScript runs in single-threaded mode in the browser. After the page is loaded, once all the JavaScript code on the page has been executed, it can only rely on trigger events to execute the JavaScript code.

After receiving the user's mouse or keyboard input, the browser will automatically trigger the corresponding event on the corresponding DOM node. If the node has been bound to the corresponding JavaScript processing function, the function will be called automatically.

JavaScript Events

The behaviors in web pages that can be detected by JavaScript are called JavaScript events. The following are some common JavaScript event examples:

1. The page content is loaded by the browser

2. The user clicks a button

3. The user presses a key

Events are usually used together with JavaScript functions, that is, event-driven functions are used to complete certain functions we want.

Common HTML events

The following is a list of some common HTML events:

Event           Description

onchange HTML element changes

onclick The user clicks on the HTML element

onmouseover The user moves the mouse on an HTML element

onmouseout The user clicks on an HTML element Move the mouse up

onkeydown The user presses the keyboard button

onload The browser has completed loading the page

onclick event

When the mouse clicks an object on the page, the onclick event will be triggered, as shown in the following example:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<script type="text/javascript">
function hello(){
    var name = document.getElementById("name").value;
    if( name == "") {
        alert("请输入你的姓名!");
        return false;
    } else {
        alert(name + ",你好!");
    }
}
</script>
</head>
<body>
  姓名:<input type="text" id="name" />
  <input type="button" onclick="hello()" value="确定" />
</body>
</html>

In the above example, the onclick event attribute is set for the OK button. The value is the "hello()" JavaScript function, that is, when the button is clicked, the hello function is executed.

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<button onclick="displayDate()">点这里</button>
<script>
function displayDate(){
document.getElementById("demo").innerHTML=Date();
}
</script>
<p id="demo"></p>
</body>
</html>

What can JavaScript do?

Events can be used to handle form validation, user input, user behavior and browser actions:

The event is triggered when the page is loaded. The event is triggered when the page is closed. The user clicks the button to perform actions to verify the legality of the user input content, etc...

You can use multiple methods to execute JavaScript event code:

HTML event attributes can directly execute JavaScript code

HTML event attributes can call JavaScript functions. You can specify your own event handler for

HTML elements.

You can prevent the occurrence of events.

etc ...


Next Section
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> function hello(){ var name = document.getElementById("name").value; if( name == "") { alert("请输入你的姓名!"); return false; } else { alert(name + ",你好!"); } } </script> </head> <body> 姓名:<input type="text" id="name" /> <input type="button" onclick="hello()" value="确定" /> </body> </html>
submitReset Code
ChapterCourseware