亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript HTML DOM events

JavaScript HTML DOM Events

HTML DOM gives JavaScript the ability to react to HTML events.

React to events

We can execute JavaScript when an event occurs, such as when the user clicks on an HTML element.

To execute code when the user clicks an element, add JavaScript code to an HTML event attribute:

onclick=JavaScript

Example of HTML event:

When the user clicks the mouse When the web page is loaded When the image is loaded When the mouse moves over the element When the input field is changed When the HTML form is submitted When the user triggers a key

In this In this example, when the user clicks, the content of the <h1> element will be changed:

<!doctype html>
<html>
<meta charset="utf-8">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus?">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
<h1 onclick="this.innerHTML='hello!'">點我點我點我!</h1>
 </body>
</html>

In this example, the function will be called from the event handler:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<script>
function changetext(id)
{
id.innerHTML="hello!";
}
</script>
</head>
<body>
<h1 onclick="changetext(this)">請點擊這段文本!</h1>
</body>
</html>

HTML Event Attribute

To assign an event to an HTML element, you can use the event attribute.

Assign an onclick event to the button element:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<body>
<p>點擊按鈕來執(zhí)行 <b>displayDate()</b> 函數(shù)。</p>
<button onclick="displayDate()">試一試</button>
<script>
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
<p id="demo"></p>
</body>
</html>

In the above example, when the button is clicked, the function named displayDate will be executed.

Using HTML DOM to assign events

HTML DOM allows you to use JavaScript to assign events to HTML elements:

Assign onclick events to button elements:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
</head>
<body>
<p>點擊按鈕來執(zhí)行 <b>displayDate()</b> 函數(shù)。</p>
<button id="myBtn">試一試</button>
<script>
document.getElementById("myBtn").onclick=function(){displayDate()};
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
<p id="demo"></p>
</body>
</html>

In the above example, the function named displayDate is assigned to the HTML element with id=myButn".

When the button is clicked, the function will be executed.

onload and onunload events

The onload and onunload events are triggered when a user enters or leaves the page.

The onload event can be used to check the visitor's browser type and version so that different versions of the web page can be loaded based on this information.

The onload and onunload events can be used to handle cookies.

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<body onload="checkCookies()">
<script>
function checkCookies()
{
if (navigator.cookieEnabled==true)
{
alert("Cookies are enabled")
}
else
{
alert("Cookies are not enabled")
}
}
</script>
<p>彈出的提示框會告訴你瀏覽器是否已啟用 cookie。</p>
</body>
</html>

onchange event

The onchange event is often used for input field validation.

The following example shows. How to use onchange. The upperCase() function is called when the user changes the content of the input field.

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<script>
function myFunction()
{
var x=document.getElementById("fname");
x.value=x.value.toUpperCase();
}
</script>
</head>
<body>
請輸入你的英文名:<input type="text" id="fname" onchange="myFunction()">
<p>當你離開輸入框時,被觸發(fā)的函數(shù)會把你輸入的文本轉(zhuǎn)換為大寫字母。</p>
</body>
</html>

onmouseover and onmouseout events

onmouseover and onmouseout events can be used when the mouse pointer moves to or away. Trigger function when element.

A simple onmouseover-onmouseout example:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<body>
<div onmouseover="mOver(this)" 
onmouseout="mOut(this)" 
style="background-color:#D94A38;width:200px;height:50px;padding-top:25px;text-align:center;">
Mouse Over Me
</div>
<script>
function mOver(obj)
{
obj.innerHTML="謝謝你"
}
function mOut(obj)
{
obj.innerHTML="把鼠標指針移動到上面"
}
</script>
</body>
</html>

onmousedown, onmouseup and onclick events

Onmousedown, onmouseup and onclick events are the entire process of mouse click. First, when a mouse button is clicked, the onmousedown event is triggered. Then, when the mouse button is released, the onmouseup event is triggered. Finally, when the mouse click is completed, the onclick event is triggered.

A simple onmousedown-onmouseup example:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<body>
<div 
onmousedown="mDown(this)" 
onmouseup="mUp(this)" 
style="background-color:#D94A38;width:200px;height:50px;padding-top:25px;text-align:center;">
點擊這里
</div>
<script>
function mDown(obj)
{
obj.style.backgroundColor="#1ec5e5";
obj.innerHTML="松開鼠標"
}
function mUp(obj)
{
obj.style.backgroundColor="#D94A38";
obj.innerHTML="謝謝你"
}
</script>
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <meta charset="utf-8"> <body> <p>點擊按鈕來執(zhí)行 <b>displayDate()</b> 函數(shù)。</p> <button onclick="displayDate()">試一試</button> <script> function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> <p id="demo"></p> </body> </html>
submitReset Code