Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/04/2015, 08:45
wilmer30
 
Fecha de Ingreso: enero-2010
Mensajes: 491
Antigüedad: 14 años, 3 meses
Puntos: 12
funciones con .attr() de Jquery

Hola,

Me encuentro en la etapa de apredizaje y me topé con este código:
Código Javascript:
Ver original
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>attr demo</title>
  6.   <style>
  7.   div {
  8.     color: blue;
  9.   }
  10.   span {
  11.     color: red;
  12.   }
  13.   b {
  14.     font-weight: bolder;
  15.   }
  16.   </style>
  17.   <script src="jquery/jquery-1.11.2.js"></script>
  18. </head>
  19. <body>
  20.  
  21. <div>Zero-th <span></span></div>
  22. <div>First <span></span></div>
  23. <div>Second <span></span></div>
  24.  
  25. <script>
  26. $( "div" )
  27.   .attr( "id", function( arr ) {
  28.     return "div-id" + arr;
  29.   })
  30.   .each(function() {
  31.     $( "span", this ).html( "(id = '<b>" + this.id + "</b>')" );
  32. });
  33. </script>
  34.  
  35. </body>
  36. </html>
y el resultado es:
Código HTML:
Ver original
  1. Zero-th (id = 'div-id0')
  2. First (id = 'div-id1')
  3. Second (id = 'div-id2')
Se ve que div-idx se incrementa, alguien por favor podría explicarme como es posbile esto con dos funciones encadenadas?

Gracias