Germán Martínez Maldonado
(GitHub: @germaaan /
Twitter: @germaaan_)
Oficina de Software Libre de la Universidad de Granada
(Twitter: @OSLUGR /
Facebook: @SoftwareLibreUGR)
Granada Geek
(http://www.meetup.com/es-ES/Granada-Geek/)
Entre otras cosas es especialmente popular por facilitar:
Usando JavaScript estándar:
document.getElementById("nombre") = "Germán";
Usando jQuery:
$("#nombre").val("Germán);
Usando JavaScript estándar:
document.getElementById("cuadro").style.background-color = "red";
document.getElementById("cuadro").style.border-color = "black";
Usando jQuery:
$("#cuadro").css({"background-color":"red", "border-color":"black"});
Usando JavaScript estándar:
OR
document.getElementById("github").addEventListener('click', cargarJSON);
Usando jQuery:
$("#github").click(function() {
...
});
Usando JavaScript estándar:
function fadeOutEffect() {
var fadeTarget = document.getElementById("cuadro");
var fadeEffect = setInterval(function () {
if (!fadeTarget.style.opacity) {
fadeTarget.style.opacity = 1;
}
if (fadeTarget.style.opacity < 0.1) {
clearInterval(fadeEffect);
} else {
fadeTarget.style.opacity -= 0.1;
}
}, 200);
}
Usando jQuery:
$("cuadro").click(function(){
$("#cuadro").fadeOut(200);
});
Usando JavaScript estándar:
function cargarJSON() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var objeto = JSON.parse(http_request.responseText);
document.getElementById("nombre") = objeto.login;
}
};
xhttp.open("GET", "https://api.github.com/users/germaaan", true);
xhttp.send();
}
Usando jQuery:
$.getJSON("https://api.github.com/users/germaaan", function(data) {
$("#nombre").val(data.login);
});
Genial, ¿eh?
bower install jQuery
$(document).ready(function(){
...
});
$(function(){
...
});
Seleccionamos un elemento y realizamos sobre él la acción que queramos.
$(selector).action()
Selector | Ejemplo | Selecciona |
---|---|---|
* | $("*") | Todos los elementos |
#id | $("#prueba") | Elementos con id "prueba" |
.class | $(".otra") | Elementos con clase "otra" |
element | $("p") | Elementos <p> |
:header | $(":header") | Todos los <h1>, <h2>, <h3>... |
:input | $(":input") | Todos los inputs |
:first | $("p:first") | Primer <p> |
:hidden | $("p:hidden") | Todos los <p> ocultos |
[attribute=value] | $("[href='index.htm']") | Todos los elementos con un atributo href igual a "index.html" |
Ratón | Teclado | Formularios | Ventana |
---|---|---|---|
click | keypress | submit | load |
dblclick | keydown | change | resize |
mouseenter | keyup | focus | scroll |
mouseleave | blur | unload |
$("form").submit(function(event){
event.preventDefault();
...
});
Usando los selectores, manipulamos elementos DOM siguiendo una metodología GET/SET.
Estoy aquí obligado para firmar
$("#cosa").html();
$("#cosa").text();
$("#nombre").val();
$("#nombre").attr("type");
Estoy aquí obligado para firmar
Estoy aquí obligado para firmar
Cualquiera
text
Estoy aquí obligado para firmar
$("#cosa").html("Estoy aquí MUY INTERESADO
");
$("#cosa").text("Estoy aquí MUY INTERESADO");
$("#nombre").val("Nadie"));
$("#nombre").attr("type", "textarea");
.alerta {
font-weight: bold;
color: red;
}
$("h1").addClass("alerta");
$("h1").removeClass("alerta");
$("h1").toggleClass("alert");
$("h1").css({"font-weight":"bold", "color":"red"});
Método | Resultado |
---|---|
hide(velocidad, callback) | Oculta el elemento si está visible. |
show(velocidad, callback) | Muestra el elemento si está oculto. |
toggle(velocidad, callback) | Alterna entre visible/oculto. |
fadeIn(velocidad, callback) | El elemento aparece a la velocidad indicada. |
fadeOut(velocidad, callback) | El elemento desaparece a la velocidad indicada. |
fadeToggle(velocidad, callback) | Alterna entre aparecer/desaparecer. |
fadeTo(velocidad, transparencia) | El elemento desaparece con una transparencia entre 0-1. |
slideDown(velocidad, callback) | Desliza el elemento hacia abajo. |
slideUp(velocidad, callback) | Desliza el elemento hacia arriba. |
slideToogle(velocidad, callback) | Alterna entre deslizar hacia abajo/arriba. |
stop() | Detiene todas las animaciones de todos los elementos. |
$("cosa").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
$("#cosa").css("background-color", "red").slideUp(2000)
.slideDown(2000);
$.ajax({url: "https://api.github.com/users/germaaan",
success: function(result){ $("#nombre").text(result.login); },
error: function(xhr){ alert("ERROR: " + xhr.status + " "
+ xhr.statusText); }
});
$.get(URL,data,function(data,status,xhr),dataType)
$.post(URL,data,function(data,status,xhr),dataType)
$.get("https://api.github.com/users/germaaan",
data: {"dato": 43},
function(data, status){
$("#nombre").text(data.login), "json";
});
$(selector).load(URL,data,function(response,status,xhr))
$("#texto").load("nombres.txt");
$().getJSON(URL,data, sucess(data,status,xhr))
$.getJSON("/API", function(result){
$.each(data, function(clave, valor) {
$("#listado").append("");
});
});
$().getScript(URL, success(response,status))
$("#boton").click(function(){
$.getScript("prueba.js");
});
jq(document).ready(function(){
jq("button").click(function(){
jq("#aviso").text("Disuélvanse, aquí no ha pasado nada.");
});
});
$("li").each(function(){
alert($(this).text())
});
var x = $("li").toArray()
for (i = 0; i< x.length; i++) {
alert(x[i].innerHTML);
}