function foo() { if (this.style) { this.style.color = 'red'; } } // No ocurre nada. window no tiene ningún atributo 'style'. foo(); // Al hacer clic sobre h1, document.getElementsByTagName('h1')[0].addEventListener('click', foo);
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <title>Título de la página</title> </head> <body> <h1>Clic sobre mí!</h1> <h2>Yo no cambio de color</h2> </body> </html>