简体   繁体   English

从事件触发Ajax完成后运行jQuery

[英]Running jQuery after ajax complete fired from event

I have the following code: 我有以下代码:

$(document).on('click', '.imageItem', function(event) {
    $(this).slideUp()
});

Whenever the .imageItem is clicked a separate script fires Ajax (I don't have control over the other script) which manipulates the DOM. 每当单击.imageItem时,都会触发一个单独的脚本来操纵DOM,而Ajax(我对其他脚本没有控制权)会触发该脚本。

So the .imageTable briefly slidesUp and hides but then it re-appears after the AJAX DOM manipulation done by the other script. 因此.imageTable会短暂地滑动并隐藏,但是在其他脚本完成AJAX DOM操作之后,它会重新出现。

I tried waiting until the Ajax was complete but this isn't working: 我尝试等待直到Ajax完成,但这不起作用:

$(document).on('click', '.imageItem', function(event) {
  $(document).ajaxComplete(function() {

    $(this).slideUp();

   });
});

I'm not sure why it's not working. 我不确定为什么它不起作用。

You probably need to keep track of which item was clicked and then use that one to hide in ajaxComplete : 您可能需要跟踪单击了哪个项目,然后使用该项目隐藏在ajaxComplete

$(document).ajaxComplete(function (event, xhr, settings) {
    if (img) {
        img.slideUp('slow');
        img = null;
    }
});

//To keep track of what item was clicked
var img = null;

$(".imageItem").on("click", function () {
    img = $(this);
});

You can see a working example in this fiddle , but as pointed by @charlietfl in comment it works if other script uses jQuery . 您可以在此小提琴中看到一个有效的示例,但正如@charlietfl在注释中指出的,如果其他脚本使用jQuery它也可以工作。

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

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