2012-04-22 138 views
0
$.post("../../Handler/Topic.ashx", 
     { commentclob: commentClob, 
     commenttitle: commentTitle, 
     topicId: id, 
     Button: buttoname }, 
     function (data) { 
     obj = jQuery.parseJSON(data); 
     var $table = $('<table/>').addClass('commentbox'); 

     $table.append('<tr><td>' + 
         'Comment Id:' + 
         obj.CommentId + 
         '</td></tr>'); 

     var includeReply = "<input type='button' 
            class='btnReply' 
            value='Reply' 
            id='btnReply-" + obj.CommentId + "' />"; 


     $("#commentContainer").prepend(
      $('<div/>').attr('id', '#comment-' + obj.CommentId) 
        .append($table) 
     ); 

     //This doesnt work   
     $("#comment-" + obj.CommentId).append(
      $('<div/>').attr('id', '#container-' + obj.CommentId)  
        .append(includeReply) 
     ); 
}); 

HTML無法創建DIV動態

<div id="commentContainer"></div> 

我能夠成功地追加與#評論-ID div來評容器,但我無法追加#comment-內的另一個DIV內的多個格ID。

我已經試過

   var str = $("<div>").attr("id", "#container-" + obj.CommentId) 
       $(str).append(includeReply); 
       $table.append('<tr><td>' + 'CommentDiv:' + str + '</td></tr>'); 

但它給

CommentDiv:[Object對象]

+0

您是否在錯誤/調試控制檯中看到任何線索? – Marc 2012-04-22 07:35:52

+0

是的,這是因爲您試圖使用字符串連接DOM節點。它可能是一串HTML,但它仍然是一個字符串。例如:[作品](http://jsfiddle.net/davidThomas/2x4F7/1/)。 [不起作用](http://jsfiddle.net/davidThomas/2x4F7/)。我建議創建'tr'和'td'就像創建'div'一樣。然後根據情況將它們附加在一起。 [這似乎工作](http://jsfiddle.net/davidThomas/2x4F7/2/)。 – 2012-04-22 07:47:15

+0

你能告訴我如何創建按鈕動態地,以便我可以追加到div – coder25 2012-04-22 07:55:30

回答

0

str是一個HTML對象,因此它可以連接在一起那樣。你必須用.html()提取標記,然後連接它。

var str = $("<div>").attr("id", "#container-" + obj.CommentId) 
    $(str).append(includeReply); 
    $table.append('<tr><td>' + 'CommentDiv:' + str.html() + '</td></tr>');