简体   繁体   English

为什么 AJAX 之后 DOM 不完整

[英]Why is the DOM incomplete after AJAX

I have an AJAX function that populates a page using an on('click')... delegate function and creates 2 or more flex containers each of which has it's own unique id.我有一个 AJAX function 使用 on('click') 填充页面...委托 function 并创建 2 个或多个它自己的唯一 flex 容器。 The id and class is displayed in each flex div block. id 和 class 显示在每个 flex div 块中。 An outline of the code is shown below:代码概要如下所示:

$('li.dicap_menu_action a').on("click",function(){
  var dicap_action =  $(this).attr("href");
  $.ajax({
    //call to php function that defines the blocks
  });
  return false; //prevent the browser from following the link
});

This works fine and output looks like this这工作正常,output 看起来像这样

创建的初始块

Next I wanted to add content to each block based on the id value of the block and then the problems started.接下来我想根据块的 id 值向每个块添加内容,然后问题就开始了。 As a proof of concept, I created a function called populate_snapshot and added the function call to the code below the AJAX function: As a proof of concept, I created a function called populate_snapshot and added the function call to the code below the AJAX function:

$('li.dicap_menu_action a').on("click",function(){
    var dicap_action =  $(this).attr("href");
    $.ajax({
        //call to php function that defines the blocks
    });
    populate_snapshot();
    return false; //prevent the browser from following the link
});

so that it executes directly after the AJAX function to replace the content in each block.以便它在 AJAX function 之后直接执行以替换每个块中的内容。 The populate_snapshot function looks like this populate_snapshot function 看起来像这样

function populate_snapshot(){
    var $flx = $('div.dicap-flex-2');
    console.log($flx);
    $flx.each(function() {
        $(this).html('This block was found');
    });
}

However, the console log does not find the flex containers and the output does not change when this runs.但是,控制台日志没有找到 flex 容器,并且 output 在运行时不会更改。

If I include an alert() into populate_snapshot as shown, then the function works fine after I dismiss the popup:如果我将 alert() 包含到 populate_snapshot 中,如图所示,则 function 在我关闭弹出窗口后工作正常:

function populate_snapshot(){
    alert('Test');
    var $flx = $('div.dicap-flex-2');
    console.log($flx);
    $flx.each(function() {
        $(this).html('This block was found');
    });
}

the console log shows the 2 flex containers and output becomes控制台日志显示 2 个 flex 容器和 output 变为

警报功能后填充确定

It is as though the DOM needs to be refreshed before the populate_snapshot function can run.就好像在 populate_snapshot function 可以运行之前刷新 DOM。 Is there some way to do this or is there something more fundamental that I am missing.有什么方法可以做到这一点,还是我缺少一些更基本的东西。 Is this an event-bubbling issue?这是一个事件冒泡问题吗? and Is there some other way to delegate the populate_snapshot function so that it runs automatically after completion of the AJAX call?还有其他方法可以委托 populate_snapshot function 以便在 AJAX 调用完成后自动运行吗?

I thought about using ajaxComplete() after the AJAX to trigger populate_snapshot, but this will not work because my populate_snapshot will later use another AJAX function and this will result in an endless loop.我曾考虑在 AJAX 之后使用 ajaxComplete() 来触发 populate_snapshot,但这不起作用,因为我的 populate_snapshot 稍后将使用另一个 AJAX ZC1C425268E68385D1AB5074F7 将导致无限循环。

Any help with this will be appreciated.对此的任何帮助将不胜感激。

Thanks to @Andreas for his advice - this answer is totally to his credit.感谢@Andreas 的建议——这个答案完全归功于他。

Here is how I used this advice to solve my problem.以下是我如何使用这个建议来解决我的问题。 The AJAX part of the code was changed as follows (notice the.then() function): AJAX 部分代码更改如下(注意.then() 函数):

$('li.dicap_menu_action a').on("click",function(){
    var dicap_action =  $(this).attr("href");
    var aj = $.ajax({
        //call to php function that defines the blocks
    });
    aj.then(function(){
      populate_snapshot();
    })
    return false; //prevent the browser from following the link
});

The populate_snapshot function is: populate_snapshot function 是:

function populate_snapshot(){
    var $flx = $('div.dicap-flex-2');
    console.log($flx);
    $flx.each(function() {
        $(this).html('This block was found');
    });
}

and the proof of concept output is as expected并且概念证明 output 符合预期

工作输出

Again, thanks and all quedos to @Andreas.再次感谢@Andreas 的所有疑问。

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

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