2012-05-12 92 views
0

我正在尝试向旧评论框添加新评论。 这里是我的代码,不知道为什么它不工作向旧评论组添加新评论

<script type="text/javascript"> 
    $(document).ready(function() { 

     $(function() { 
      $(".bsubmit").click(function() { 
       var id = $(this).parent().parent().attr("id"); 
       var comm = document.getElementById(id).getElementsByClassName("commentadd")[0].value; 
       $.ajax({ 
        type: "POST", 
        url: "comment.php", 
        data: { 
         id: id, 
         comm: comm 
        }, 
        cache: false, 
        success: function() { 
         $('.addcomment').slideUp('slow', function() { 

          // POSTED IN DATABASE SUCCESS NOW APPEND The comment with other COMMENTS 

          document.getElementById(id).getElementsByClassName(".itemcomment").append('<div class="comm"> 
    <a href="profile.php?id=<?php echo $_SESSION[' 
          fbid ']; ?> class="comm_img"><img src=" <?php echo $_SESSION[' 
          smallest ']; ?> width="50" height="50" /></a> 
    <p width="50"><strong> 
    </strong></p> 
    </div>'); 

         }); 
         $('#load').fadeOut(); 
        } 
       }); 
       return false; 
      }); 
     }); 
    }); 
</script> 
+2

尝试使用'$(“#div_id .itemcomment”)'而不是'document.getElementById(id).getElementsByClassName(“。itemcomment”)'。 – Karo

+0

@David - 但是当PHP执行时,这将写入javescript。 假设存储在一个变量中。现在我只是追加变量的值..不是吗? – Yahoo

+0

@Karo - 我的Div_ID位于一个名为ID的变量中。我怎么可以重新创建该声明? 谢谢 – Yahoo

回答

1

我认为错误是在“效果基本show”回调中的代码,所以我觉得做正确的做法是:

使用常规的JavaScript:

$('.addcomment').slideUp('slow', function() { 

    var elem = getElementById(id).getElementsByClassName(".itemcomment")[0], 
     div = document.createElement("div"), 
     a = document.createElement("a"), 
     p = document.createElement("p"), 
     img = document.createElement("img"), 
     strong = document.createElement("strong"); 

     div.className = "comm"; 
     a.className = "comm_img"; 
     a.href = "profile.php?id=<?php echo $_SESSION['fbid ']; ?>"; 
     img.src = "<?php echo $_SESSION['smallest ']; ?>"; 
     img.width = "50"; 
     img.height = "50"; 
     p.width = "50"; 

     p.appendChild(strong); 
     a.appendChild(img); 
     div.appendChild(a); 
     div.appendChild(p); 
     elem.appendChild(div); 
}); 

使用jQuery:

$('.addcomment').slideUp('slow', function() { 
    var html = '<div class=comm>'; 
    html += '<a href="profile.php?id=' + "<?php echo $_SESSION['fbid'];?>" + 'class=comm_img>'; 
    html += '<img src="' + "<?php echo $_SESSION['smallest']; ?>" + 'width="50" height="50" /></a>'; 
    html += '<p width="50"><strong></strong></p></div>'; 

    $(".itemcomment", "#"+id).append(html);   
}); 

希望HEL PS!