2010-09-21 25 views
1

我有一個情況IM apending HTML加載更多帖子(分頁),每個崗位都有一個回覆鏈接,該appened HTML是不正常:append.html jquery與現有的html不一樣嗎?

jQuery的崗位:

$(function() { 
    //More Button 
    $('.more').live("click", function() { 
     var ID = $(this).attr("id"); 
     if (ID) { 
      $("#more" + ID).html('<img src="moreajax.gif" />'); 

      $.ajax({ 
       type: "POST", 
       url: "ajax_more.php", 
       data: "lastmsg=" + ID, 
       cache: false, 
       success: function (html) { 
        $("ul.statuses").append(html); 
        $("#more" + ID).remove(); 
       } 
      }); 
     } else { 
      $(".morebox").html('The End'); 
     } 

     return false; 

    }); 
}); 

HTML文件:

return <<<ENDOFRETURN 
    <li> 
     <a href="nano.com/$username"><img class="avatar" src="images/$picture" width="48" height="48" alt="avatar" /></a> 
     <div class="tweetTxt"> 
      <strong><a href="nano.com/$username">$username</a></strong> $auto 
      <div class="date">$rel</div> 
      <a class ="reply" href="[email protected]$username&status_id=$id&reply_name=$username"> reply </a> 
     </div> 
     <div class="clear"></div> 
    </li> 
ENDOFRETURN; 

回覆鏈接的jQuery:

function insertParamIntoField(anchor, param, field) { 
    var query = anchor.search.substring(1, anchor.search.length).split('&'); 

    for (var i = 0, kv; i < query.length; i++) { 
     kv = query[i].split('=', 2); 
     if (kv[0] == param) { 
      field.val(kv[1]); 
      return; 
     } 
    } 
} 

$(function() { 
    $("a.reply").click(function (e) { 

     insertParamIntoField(this, "status_id", $("#status_id")); 
     insertParamIntoField(this, "reply_name", $("#reply_name")); 
     insertParamIntoField(this, "replyto", $("#inputField")); 

     $("#inputField").focus() 
     $("#inputField").val($("#inputField").val() + ' '); 

     e.preventDefault(); 
     return false; // prevent default action 
    }); 
}); 
+0

額外的信息:當我加載更多內容,點擊回覆按鈕,頁面refershes! – getaway 2010-09-21 10:57:36

+0

你永遠不會定義什麼「不能正常工作」的意思...... – 2010-09-21 11:00:29

+0

對不起,基本上當我點擊回覆鏈接時,它採用用戶名和插入到文本框中,但是當我使用append.html加載更多帖子時,該回復鏈接deosnt工作!所以我認爲它的東西做的附加HTML – getaway 2010-09-21 11:03:27

回答

1

我在黑暗中拍攝,但聽起來好像您還沒有在回覆按鈕上使用.live()事件。任何附加到文檔的新按鈕都不會有您爲它們指定的單擊事件。

所以總之,確保任何動態加載的按鈕和需要觸發事件的按鈕都使用「活動」事件綁定。

+0

多數民衆贊成多數民衆贊成我曾嘗試使用生活()按鈕,但是,這個deosnt似乎工作以及!即時將要告訴你的代碼吧! – getaway 2010-09-21 11:16:59

+0

yeh it deos work,cheers mate,我以錯誤的方式使用它 – getaway 2010-09-21 11:25:58

+0

NP,輕鬆完成:) – roborourke 2010-09-22 08:51:03

1

更改click處理程序使用.live()這樣使它在以後添加以及:聯繫工作

$(function() { 
    $("a.reply").live("click", function (e) { 
    insertParamIntoField(this, "status_id", $("#status_id")); 
    insertParamIntoField(this, "reply_name", $("#reply_name")); 
    insertParamIntoField(this, "replyto", $("#inputField")); 
    $("#inputField").val(function(i, v) { return v + ' '; }).focus(); 
    return false; 
    }); 
});