Created
June 9, 2015 15:26
-
-
Save eamexicano/129331093270c9486d0a to your computer and use it in GitHub Desktop.
Atributos data - Objetos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Utilizar atributos data para almacenar contenido | |
--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<h1 data-mensaje="Mensaje dentro del HTML"> Click!</h1> | |
<script> | |
function mostrar_mensaje () { | |
alert(this.dataset.mensaje); | |
} | |
var nodo = document.getElementsByTagName('h1')[0]; | |
nodo.addEventListener('click', mostrar_mensaje, false); | |
</script> | |
</body> | |
</html> | |
<!-- | |
Utilizar objetos para almacenar contenido | |
--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<h1> Click!</h1> | |
<script> | |
var contenido = { | |
mensaje: "Mensaje en un objeto." | |
}; | |
function mostrar_mensaje () { | |
alert(contenido.mensaje); | |
} | |
var nodo = document.getElementsByTagName('h1')[0]; | |
nodo.addEventListener('click', mostrar_mensaje, false); | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment