Javascript
De 33system wiki
Contenido
Javascript
Teoría
Práctica
Enlazar un script externo
En cualquier parte del body.
<script src="myScript.js"></script>
Sustituir una cadena de caracteres
document.getElementById("tagid").innerHTML = "New String."
Lanzar un script con un botón
<button type="button" onclick="myFunction()">Try it</button>
<script> function myFunction() { document.getElementById("tagid").innerHTML = "New String."; } </script>
Declarar variables
<script> // definimos el valor de x var x = 5; // definimos el valor de y var y = x + 2; // sustituimos el valor del tag por el de la variable document.getElementById("tagid").innerHTML = y; </script>
Cambiar estilo css
<script> document.getElementById("p2").style.color = "blue"; </script>
Cambiar atributos de un tag html
<script> // document.getElementsByTagName("nombre_del_tag")[posicion].setAttribute("atributo","nuevo_valor") document.getElementsByTagName("INPUT")[0].setAttribute("type","button"); </script>
Mostrar mensaje en pantalla
<script> $(document).click(function() { alert("me"); }); $(".TargetID").click(function(e) { e.stopPropagation(); // This is the preferred method. return false; // This should not be used unless you do not want // any click events registering inside the div }); </script>
Ocultar elemento al hacer click
<script type="text/javascript"> <!-- function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } //--> </script> <a href="#" onclick="toggle_visibility('ID');"><div></div></a>