執行 ❯
建立您
自己的
網站
×
更改方向
儲存程式碼
更改主題,深色/淺色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript "this"</h1> <p>This example demonstrate that in a regular function, the "this" keyword represents different objects depending on how the function was called.</p> <p>Click the button to execute the "hello" function again, and you will see that this time "this" represents the button object.</p> <button id="btn">Click Me!</button> <p id="demo"></p> <script> let hello = ""; hello = function() { document.getElementById("demo").innerHTML += this; } //The window object calls the function: window.addEventListener("load", hello); //A button object calls the function: document.getElementById("btn").addEventListener("click", hello); </script> </body> </html>