[英]New HTML Data Append cannot be selected by Jquery
我附加了從Ajax接收到的數據,它也包含帶有bsubmit類的除法。 當頁面加載時,如果我單擊包含類bsubit的div,則執行以下函數,但是當我單擊也包含類bsubmit的新附加數據時,以下代碼不起作用。
這是代碼
$(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(data){
$('.addcomment').slideUp('slow', function() {
});
// Bottom Line not working
$("#"+id).find(".item_comment").append(data); // DATA Contains a div with class bsubmit
$(data).appendTo("#"+id).find(".item_comment"); // Now these Appended Div isnt calling this function again, Why ? I want to call them again
$('#load').fadeOut();
}
});
return false;
});
});
如果我理解您的問題:您是否嘗試過.on()
方法? (jQuery 1.7)
$(yourParentElement).on('click','.bsubmit',function() {
appendTo
不返回父級。
$("#"+id).find(".item_comment").append(data);
$(data).appendTo("#"+id).parent().find(".item_comment"); // Use "parent()"
$('#load').fadeOut();
但是您確實應該緩存一些DOM元素...
var id = $(this).parent().parent().attr("id");
改成:
var $parent = $(this).parent().parent();
接着:
$("#"+id).find(".item_comment").append(data);
改成:
$parent.find(".item_comment").append(data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.