简体   繁体   English

当replaceWith完成加载时运行javascript函数

[英]Run javascript function when replaceWith finishes loading

I have jQuery replaceWith call, and I want to pop up an alert only when the replaceWith finishes loading . 我有jQuery replaceWith调用,并且仅在replaceWith完成加载时才想弹出警报

To achieve this I have this very naive javascript implementation: 为此,我有一个非常幼稚的javascript实现:

 $(document).ready(function (){ $("#myDiv").click(function () { $("#myDiv").replaceWith("<div>Hello World!!!</div>"); alert("done"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myDiv">Hello!</div> 

The problem is that in this case the alert will pop-up independently of the time that the replaceWith takes. 问题在于,在这种情况下,警报将独立于replaceWith花费的时间弹出。 If it is fast, no problem, but if the replaceWith takes several seconds to load (which is the real case) then the pop-up appears way before, and I want to avoid that. 如果速度很快,没问题,但是如果replaceWith需要几秒钟的时间加载(这是真实情况),那么弹出窗口就会出现在前面,我想避免这种情况。

How can I achieve the behaviour I am looking for? 如何实现我想要的行为?

Try 尝试

$(document).ready(function() {
  var body = $("body");
  $("#myDiv").click(function(e) {
    var html =  $("<div>Hello World!!!</div>");
    $("#myDiv").replaceWith(html)
      .promise().done(function(elem) {
        if (body.find(html).is("*") 
            && !body.find(elem).is("*")) {
               alert("done");
        }
      });    
  });
});

 $(document).ready(function() { var body = $("body"); $("#myDiv").click(function(e) { var html = $("<img src=http://lorempixel.com/output/cats-qc-1920-1920-2.jpg />"); // $("<div>Hello World!!!</div>"); $("#myDiv").replaceWith(html) .promise().done(function(elem) { if (body.find(html).is("*") && !body.find(elem).is("*")) { alert("done"); } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myDiv">Hello!</div> 

I would try getting the value of what you're replacing - then check if it exists after the replacement - then you can alert its complete. 我会尝试获取您要替换的内容的值-然后在替换后检查它是否存在-然后您可以提醒其完成情况。

fiddle: http://jsfiddle.net/eavxkc3f/ 小提琴: http//jsfiddle.net/eavxkc3f/

jquery: jQuery的:

$(document).ready(function (){
  $(".myDiv").click(function () {

      var currentItem = $(".myDiv").html();
      var replacer = "<div>Hello World!!!</div>";

      $(".myDiv").replaceWith("<div>Hello World!!!</div>");

      if($(".myDiv").html != currentItem){
          alert("Done.");
      }

  });
});

Take a look at the DOM MutationObserver spec, I think it does what you want. 看一下DOM MutationObserver规范,我认为它可以满足您的要求。 Register it on a target node, and it will watch for changes beneath that target. 在目标节点上注册它,它将监视该目标下的更改。

It's an updated version of the now deprecated Mutation events 这是现已弃用的Mutation事件的更新版本

A blog post with additional good info (and where I found this sample code) 博客文章,其中包含其他良好的信息 (以及在其中找到此示例代码的地方)

Working example in this fiddle (code below) 这个小提琴中的工作示例(下面的代码)

$(document).ready(function (){
  $("#myDiv").click(function () {
    $("#myDiv").replaceWith("<div>Hello World!!!</div>");
  });
});

// select the target node
var target = document.querySelector('#divParent');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    //console.log(mutation.type);
      alert('Done');
  });
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
//observer.disconnect();

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

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