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

在replaceWith完成加载时运行javascript函数,javascript,jquery,Javascript,Jquery,我有jQueryreplaceWith调用,我只想在replaceWith完成加载时弹出一个警报 为了实现这一点,我有一个非常简单的javascript实现: $(文档).ready(函数(){ $(“#myDiv”)。单击(函数(){ $(“#myDiv”)。替换为(“你好,世界!!!”; 警惕(“完成”); }); }); 你好!我会尝试获取您要替换的内容的值,然后检查它在替换后是否存在,然后您可以通知它已完成 小提琴: jquery: $(document).ready(functi

我有jQuery
replaceWith
调用,我只想在replaceWith完成加载时弹出一个警报

为了实现这一点,我有一个非常简单的javascript实现:

$(文档).ready(函数(){
$(“#myDiv”)。单击(函数(){
$(“#myDiv”)。替换为(“你好,世界!!!”;
警惕(“完成”);
});
});


你好!我会尝试获取您要替换的内容的值,然后检查它在替换后是否存在,然后您可以通知它已完成

小提琴:

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.");
      }

  });
});
$(文档).ready(函数(){
$(“.myDiv”)。单击(函数(){
var currentItem=$(“.myDiv”).html();
var replacer=“你好,世界!!!”;
$(“.myDiv”).replaceWith(“你好,世界!!!”);
if($(“.myDiv”).html!=currentItem){
警惕(“完成”);
}
});
});
试试看

$(文档).ready(函数(){
主体变量=$(“主体”);
$(“#myDiv”)。单击(函数(e){
var html=$(“你好,世界!!!”);
$(“#myDiv”).replacetwith(html)
.promise().done(函数(elem){
if(body.find(html).is(“*”)
&&!body.find(elem).is(“*”){
警惕(“完成”);
}
});    
});
});
$(文档).ready(函数(){
主体变量=$(“主体”);
$(“#myDiv”)。单击(函数(e){
var html=$(“”);
//$(“你好,世界!!!”);
$(“#myDiv”).replacetwith(html)
.promise().done(函数(elem){
if(body.find(html).is(“*”)
&&!body.find(elem).is(“*”){
警惕(“完成”);
}
});
});
});

你好!看看规范,我认为它符合您的要求。在目标节点上注册它,它将监视该目标下的更改

它是现在已弃用的更新版本

A(我在哪里找到了这个示例代码)

中的工作示例(下面的代码)

$(文档).ready(函数(){
$(“#myDiv”)。单击(函数(){
$(“#myDiv”)。替换为(“你好,世界!!!”;
});
});
//选择目标节点
var target=document.querySelector(“#divParent”);
//创建一个观察者实例
var观察者=新的突变观察者(功能(突变){
突变。forEach(功能(突变){
//console.log(mutation.type);
警报(“完成”);
});
});
//观察员的配置:
var config={attributes:true,childList:true,characterData:true};
//传入目标节点以及观察者选项
observer.observe(目标,配置);
//稍后,你可以停止观察
//observer.disconnect();

您是否可以添加
replaceWith
需要花费大量时间的情况?即使如此,JavaScript还是单线程的,因此在函数返回之前不会出现警报。将“几秒钟操作”定义为承诺,然后移动
警报(或任何您想采取的操作)在其“done”回调中。@JamesThorpe:问题是,对服务器的调用将返回
replaceWith
使用的数据,这需要几秒钟,而且很大。因此,由于获取数据,然后将其加载到客户端,因此总是会有延迟。假设您正在获取一个大图像并加载到客户机上。在对图像进行任何更改之前,您希望完全加载图像:D@raina77ow:我对jQuery和javascript这件事还很陌生。什么是承诺?我怎样才能使用一个呢?啊,现在我们有点进展了:)正如@raina77ow所说的-你需要把你的代码放在获取数据的调用的回调中
replaceWith
本身并不需要几秒钟的时间,这是您在此之前所做的任何事情。
$(“.myDiv”).html
是一个函数,它总是不等于字符串
$(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 (){
  $("#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();