运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>Arrow Function</h1> <p>The <strong>this</strong> keyword represents the Header object.</p> <button id="btn">Click Me!</button> <p><strong>this</strong> represents:</p> <p id="demo"></p> <script> class Header { constructor() { this.color = "Red"; } changeColor = () => { document.getElementById("demo").innerHTML += this; } } const myheader = new Header(); //The window object calls the function: window.addEventListener("load", myheader.changeColor); //A button object calls the function: document.getElementById("btn").addEventListener("click", myheader.changeColor); </script> </body> </html>