Ver Mensaje Individual
  #14 (permalink)  
Antiguo 30/03/2012, 15:17
Avatar de Celcius
Celcius
 
Fecha de Ingreso: febrero-2003
Ubicación: Lima - Perú
Mensajes: 652
Antigüedad: 21 años, 2 meses
Puntos: 5
Respuesta: Problema explicado en video: Asignar eventos a objetos creados dinámicamen

Ok aquí va el codigo HTML:

Código HTML:
Ver original
  1. <div id="izquierda">
  2.         <div id="form-posteo">
  3.             <form action="#" method="post" name="nuevo_post" id="nuevo_post">
  4.                     <textarea name="post" id="post" class="post">Publicar nuevo mensaje en el Muro...</textarea>
  5.                     <div class="boton"><input type="submit" name="enviar" value="Publicar" /></div>
  6.             </form>
  7.         </div>
  8.         <div id="count-caracteres"></div>
  9.         <div id="datos-personales">
  10.             <div>
  11.                 <p><b>Usuario:</b> <i>No has elegido un nombre de usuario</i></p>
  12.                 <p><b>Colegio:</b> <i>No definido</i></p>
  13.             </div>
  14.         </div>
  15.     </div>
  16.     <div id="derecha">
  17.         <div id="posteos">
  18.             <?php
  19.             $con = new conexion();
  20.             $consulta = $con->Consulta("SELECT sm_post.*, sm_usuario.nombre_usuario FROM sm_post INNER JOIN sm_usuario ON sm_post.id_usuario=sm_usuario.id_usuario WHERE sm_usuario.id_usuario='". $_SESSION['id'] ."' ORDER BY sm_post.id DESC LIMIT 0,10");
  21.             while($datos = mysql_fetch_assoc($consulta)){
  22.                 ?>
  23.                 <div class="publicacion">
  24.                     <header>
  25.                         <div class="titulo-user"><i>@<?=$datos["nombre_usuario"] ?></i> <span class="escribio">escribi&oacute;</span>:</div>
  26.                         <div class="titulo-fecha"><?=$datos["fecha"] ?></div>
  27.                     </header>
  28.                     <div class="post-contenido">
  29.                         <?=$datos["post"]?>
  30.                     </div>
  31.                     <div id="comentarios-post-<?=$datos["id"]?>">
  32.                         <h3>Comentarios</h3>
  33.                         <?php
  34.                         $con = new conexion();
  35.                         $consulta2 = $con->Consulta("SELECT sm_comentario.*, sm_usuario.nombre_usuario FROM sm_comentario INNER JOIN sm_usuario ON sm_comentario.id_usuario=sm_usuario.id_usuario WHERE sm_usuario.id_usuario='". $_SESSION['id'] ."' and id_post='". $datos["id"] ."' ORDER BY sm_comentario.id_comentario ASC");
  36.                         while($datos2 = mysql_fetch_assoc($consulta2)){
  37.                             ?>
  38.                             <div class="comment-user">
  39.                                 <header>
  40.                                     <div>
  41.                                         <i><b>@<?=$datos2["nombre_usuario"] ?></b></i>
  42.                                         <span class="escribio">comentó</span>:
  43.                                     </div>
  44.                                 </header>
  45.                                 <div class="comment-contenido">
  46.                                     <?=$datos2["comentario"]?>
  47.                                 </div>
  48.                             </div>
  49.                             <?
  50.                         }
  51.                         ?>
  52.                     </div>
  53.                     <div class="form-comment">
  54.                         <form action="#" name="form-comment-<?=$datos["id"]?>" method="post" id="form-comment-<?=$datos["id"]?>">
  55.                             <p class="caja-comment"><textarea name="input-comment-<?=$datos["id"]?>" id="input-comment-<?=$datos["id"]?>" class="ingreso-comentario">Escribe un comentario...</textarea></p>
  56.                         </form>
  57.                     </div>
  58.                 </div>
  59.                 <?
  60.             }
  61.             ?>
  62.         </div>
  63.     </div>

Notas, las clases izquierda y derecha corresponden a la parte donde se ingresan los post (izquierda) y al lugar donde estarán los post (derecha).

JQuery

nuevo_post.js

Código Javascript:
Ver original
  1. $(document).ready(function(){
  2.     $("#post").click(function(){
  3.         $("#post").fadeIn('slow', function(){
  4.             $("#post").css("height","50px");
  5.             $("#post").css("color","#464646");
  6.             $(".boton").css("display","block");
  7.             if($("#post").val()=="Publicar nuevo mensaje en el Muro..."){
  8.                 $("#post").val("");
  9.             }
  10.         });
  11.     });
  12.  
  13.     $("#post").keypress(function(event){
  14.         // tamaño
  15.         var tam = $("#post").val().length;
  16.         // variable para colocar el mensaje del numero de caracteres
  17.         var num_car = "<b style=\"color: black\">Estas ingresando <span>"+ tam +"</span> caracteres</b>";
  18.         if(tam>100 && tam<=140){
  19.             num_car = "<b style=\"color: orange\">Estas ingresando <span>"+ tam +"</span> caracteres</b>";
  20.         }else if(tam>140){
  21.             num_car = "<b style=\"color: red\">Estas ingresando <span>"+ tam +"</span> caracteres</b>";
  22.         }
  23.         $("#count-caracteres").html(""+ num_car +"");
  24.     });
  25.     // si el clic del mouse sale del textarea entonces vuelve a su estado normal
  26.     $("#nuevo_post").submit(function(){
  27.         if($("#post").val()=="" || $("#post").val()=="Publicar nuevo mensaje en el Muro..." ){
  28.             $("#mensaje").html("No puede dejar vacio el campo de texto");
  29.             $("#mensaje").focus();
  30.             $("#mensaje").dialog({
  31.                 modal   : true,
  32.                 title   : "Encontramos un error",
  33.                 buttons : {
  34.                     "Ok": function(){
  35.                         $(this).dialog("close");
  36.                     }
  37.                 }
  38.             });
  39.         }else{
  40.             $.ajax({
  41.                 data        : $(this).serialize(),
  42.                 type        : "POST",
  43.                 dataType    : "json",
  44.                 url         : "ingresar_post.php",
  45.                 success: function(data){
  46.                     if(data.tipo=='error'){
  47.                         $("#mensaje").html("No se ha podido ingresar el Post");
  48.                         $("#mensaje").dialog({
  49.                             modal   : true,
  50.                             title   : "Encontramos un error",
  51.                             buttons : {
  52.                                 "Ok": function(){
  53.                                     $(this).dialog("close");
  54.                                 }
  55.                             }
  56.                         });
  57.                     }else{
  58.                         // Buscamos en la base de datos y colocamos en el primer cuadro:
  59.                         $.ajax({
  60.                             url         : "extraer_ultimo_post.php",
  61.                             dataType    : "json",
  62.                             success     : function(data){
  63.                                 $("#posteos").prepend("<div class=\"publicacion\"><header><div class=\"titulo-user\"><i>@"+ data.nombre_usuario +"</i> <span class=\"escribio\">escribió</span>:</div><div class=\"titulo-fecha\"></div></header><div class=\"post-contenido\">"+ data.post +"</div><div id=\"comentarios-post-"+ data.id +"\"><h3>Comentarios</h3></div><div class=\"form-comment\"><form action=\"#\" name=\"form-comment-"+ data.id +"\" method=\"post\" id=\"form-comment-"+ data.id +"\"><p class=\"caja-comment\"><textarea name=\"input-comment-"+ data.id +"\" id=\"input-comment-"+ data.id +"\" class=\"ingreso-comentario\">Escribe un comentario...</textarea></p></form></div></div>");
  64.                                 $("#post").css("height","25px");
  65.                                 $("#post").css("color","#A0A0A0");
  66.                                 $("#post").val("Publicar nuevo mensaje en el Muro...");
  67.                             }
  68.                            });
  69.                     }
  70.                 }
  71.                });
  72.         }
  73.         return false;
  74.     });
  75. });

/*function limpiarCaja(elemento){
if($("#"+ elemento.id +"").val()!="Escribe un comentario..."){
$("#"+ elemento.id +"").val("");
$("#"+ elemento.id +"").css("color","black");
}
}*/

nuevo_comment.js

Código Javascript:
Ver original
  1. $(document).ready(function(){
  2.     $('.caja-comment').on('.ingreso-comentario','click',function(){
  3.         if($(this).val()=="Escribe un comentario..."){
  4.             $(this).val("");
  5.         }
  6.         $(this).css("color","black");
  7.     });
  8.  
  9.     /*$(".caja-comment").live("click", ".ingreso-comentario", function(){
  10.         if($(this).val()=="Escribe un comentario..."){
  11.             $(this).val("");
  12.         }
  13.         $(this).css("color","black");
  14.     });*/
  15.     /*$(".caja-comment textarea").click(function(){
  16.         //alert($(this).attr("id"));
  17.         if($(this).val()=="Escribe un comentario..."){
  18.             $(this).val("");
  19.         }
  20.         $(this).css("color","black");
  21.     });*/
  22.     $(".caja-comment").on("keyup",".ingreso-comentario", function(event){
  23.         if(event.keyCode=='13'){
  24.         // cuando deseemos enviar el comentario entonces capturamos el idenficador del post.
  25.         var identificador   = $(this).attr("id");
  26.         var arreglo         = identificador.split("-");
  27.         var id              = arreglo[2]; // variable que almacena el ID del POST
  28.  
  29.             // recogemos el valor de dicho campo
  30.             var comentario = $(this).val();
  31.             $(this).val("");
  32.             $.ajax({
  33.                 data        : "comment="+ comentario +"&id_post="+ id,
  34.                 type        : "POST",
  35.                 dataType    : "json",
  36.                 url         : "ingresar_comentario.php",
  37.                 success: function(data){
  38.                     if(data.tipo=='error'){
  39.                         $("#mensaje").html("No se ha podido ingresar el comentario");
  40.                         $("#mensaje").dialog({
  41.                             modal   : true,
  42.                             title   : "Encontramos un error",
  43.                             buttons : {
  44.                                 "Ok": function(){
  45.                                     $(this).dialog("close");
  46.                                 }
  47.                             }
  48.                         });
  49.                     }else{
  50.                         // Buscamos en la base de datos y colocamos en el primer cuadro:
  51.                         $.ajax({
  52.                             url: "extraer_ultimo_comentario.php",
  53.                             dataType: "json",
  54.                             success: function(data){
  55.                                 $("#comentarios-post-"+id).append("<div class=\"comment-user\"><header><div><i><b>@"+ data.nombre_usuario +"</b></i> <span class=\"escribio\">comentó</span>:</div></header><div>"+ data.comentario +"</div></div>");
  56.                             }
  57.                            });
  58.                     }
  59.                 }
  60.             });
  61.  
  62.         }
  63.     });
  64.    
  65.     $(".caja-comment textarea").blur(function(){
  66.         if($(this).val()==""){
  67.             $(this).css("color","#A8C2D7");
  68.             $(this).val("Escribe un comentario...");
  69.         }
  70.     });
  71. });

Si necesitan el PHP me avisan (lo dudo). Gracias por su tiempo!
__________________
"Si tú conocieras el don de Dios, y quién es el que te dice: 'Dame de beber,' tú Le habrías pedido a El, y El te hubiera dado agua viva.
Sn. Juan 4:19
Jesus