运行 ❯
获取您
自己
的网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 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>