如何在页面加载后动态生成的DOM对象上触发事件

时间:2013-07-02 14:34:20

标签: javascript jquery jquery-selectors pageload

当页面的一部分加载了动态创建的按钮时,我无法激活相关的JavaScript / jQuery事件。 我该如何解决这个问题?解决此问题的最佳方法是什么?

这是基于我在实际情况下的麻烦。

我有这个函数调用php函数在Ajax页面中生成一个新的动态创建分页部分。

function loadData(page){
        $.ajax({
            type: "POST",
            url: ".../get_Scorewithagination.php",
            data: 'page='+page+'&testID='+testID+'&testDate='+testDate+'&empPass='+empPass+'&courseCode='+courseCode,
            success: function(msg){
                $('#testInfo').empty();
                $(document).ajaxComplete(function(event, request, settings){
                    loading_hide();
                    $("#resultBox").html(msg); to #resultBox
                });

                $('.iconpoint_orange').click(function() {
                    btnNames = $(this).attr("name").split("_");
                    $.post('.../getInfo.php', {testresult: btnNames[1]}, ShowTestInfo); 
                });

                // Collapse row on delete
                $('.iconpoint_blue').click(function() {
                    alert();
                    rowName = this.name;
                    rowID = '#' + rowName;
                    $(this).closest('tr').children('td').addClass("row_hover");

                    $("#dialog_confirm").html("Are you sure you want to delete this?");
                    $("#dialog_confirm").dialog({
                          buttons : {
                        "Delete" : function() { 
                        $(rowID).closest('tr').animate({opacity: 0},500).slideUp(300);
                        setTimeout(function() {$(rowID).closest('tr').empty().remove();} ,3000);
                        $(this).dialog("close");

                        },
                        "Cancel" : function() {
                        $(rowID).closest('tr').children('td').removeClass("row_hover");
                        $(this).dialog("close");
                        }
                          }
                    });
                $("#dialog_confirm").dialog("open"); // Show dialog box

                });

            },
            error: function(XMLHttpRequest, textStatus, errorThrown) { 
                        alert("Error: " + errorThrown); 
                        $('#testInfo').html("<br\>Status: " + textStatus +"<br\>Error: " + errorThrown);
                           $('#testInfo').addClass("errorRed");


            }       

        });//$.ajax() END
    }//loadData(page) END

但问题是那两个按钮

.iconpoint_blue

.iconpoint_orange
在用Ajax调用的php文件动态生成行之后,

无法调用响应。

我知道Ajax返回的html代码在文档完成之后不会被JavaScript扫描,因为它在以后完成,因此,我动态创建的按钮即使是正确的选择器也不会响应该函数{{1} }。

我希望动态生成的按钮具有现有函数的每个记录的选择器,是否能够回调在页面加载时加载的函数?

1 个答案:

答案 0 :(得分:4)

将函数绑定到父元素。例如,使用此HTML:

<div class='wrapper'>
    <a href='#' class='click_here'>click here</a>
</div>

您可以使用以下javascript:

$('.wrapper').on( 'click', '.click_here', function() {
    $(this).after('<a href="#" class="or_here">or here</a>');
});
$('.wrapper').on( 'click', '.or_here', function() {
    $(this).remove();
});

Here是一个jsFiddle。