iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0

For the topic today, I can't fully understand the logic that it works.
So forgive me if I don't explain that clearly. It takes time to digest the knowledge before outputting an understandable article.

Simple example of event handling

https://ithelp.ithome.com.tw/upload/images/20210924/20130362rR0Mkmm5hh.png
https://ithelp.ithome.com.tw/upload/images/20210924/20130362oZSIAA3DFY.png

<!DOCTYPE html>
<html>
<head>
<title>JS - HTML DOM Events - Part 2-1</title>
<script type="text/javascript">
function over(element){
    element.style.color="orange";
}
function out(element){
   element.style.color="black"; 
}
</script>
</head>
<body style="font-family:Arial";>
    <button onclick="alert('Clicked');">Click</button> <!-- Registering an event -->
    <span onmouseover="over(this);"
        onmouseout="out(this);">Get your cursor here to know my real color.</span>
    <div onmouseover="over(this);"
        onmouseout="out(this);">Closer, closer, closer!</div>
        
</body>
</html>

When it comes to event handling, “this” represents the ..., ... objects.

Advanced example of event handling

<!DOCTYPE html>
<html>
<head>
<title>JS - HTML DOM Events - Part 2-2</title>
<script type="text/javascript">
// Advanced way to register an event dynamically through JS.
function init(){ //init = initialization
    var btn=document.getElementById("btn");
    var handler=function(){ // prepare a event listener, aka a event handler
        alert("Clicked");
    }
    btn.addEventListener("click"/*event name*/, handler);
}
</script>
</head>
<body onload="init();">  <!-- Statically register the "load" event on <body>. Onload event will be initialized once the webpage is fully loaded. -->
    <button id="btn">Click</button>
 
        
</body>
</html>

Music of Today: The List by Moonchild

Yes


Like/Share/Follow

Feel free to comment and share your ideas below to learn together!
If you guys find this article helpful, please kindly do the writer a favor — LIKE this article./images/emoticon/emoticon12.gif


上一篇
#23 JS: HTML DOM Events - Part 1
下一篇
#25 JS: HTML DOM Events - Part 3(Start Over Version)
系列文
Learn & Play JavaScript -- Entry-Level Front-End Web Development30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言