简体   繁体   English

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

[英]How to fire an event on DOM objects dynamically generated after page load

I had trouble to activate related JavaScript / jQuery event when the part of the page is loaded with dynamically created button. 当页面的一部分加载有动态创建的按钮时,我无法激活相关的JavaScript / jQuery事件。 How can I resolve this issue? 我该如何解决这个问题? What is the best method to resolve this issue? 解决此问题的最佳方法是什么?

It is base on my trouble in real case. 这实际上是基于我的麻烦。

I have this function to call the php function to generate a new dynamically create pagination section in a page with Ajax. 我有这个函数来调用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

But the issue is that those two buttons 但是问题是那两个按钮

.iconpoint_blue

and

.iconpoint_orange

are not be able to call for a respond after the rows are dynamically generated by php file I called with Ajax. 行由我用Ajax调用的php文件动态生成后,将无法调用响应。

I know that Ajax returned html codes are not scanned by JavaScript at the beginning of the document complete since it was completed later, therefore, the button I created dynamically will not respond to the function even its the correct selector # . 我知道,由于Ajax返回的html代码是在稍后完成的,因此它不会在文档开始时被JavaScript扫描,因此,我动态创建的按钮即使正确的选择器#也不会响应该函数。

I want the dynamically generated button with existing function's selector for each record, will be able to call back the function that is loaded at page load? 我希望动态生成的按钮具有每个记录的现有函数的选择器,将能够回调在页面加载时加载的函数吗?

Bind the function to a parent element. 将函数绑定到父元素。 For example, with this HTML: 例如,使用以下HTML:

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

you could have the following javascript: 您可以使用以下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 'sa jsFiddle. 是一个jsFiddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM