Javascript 重新加载引导模式内容延迟问题

Javascript 重新加载引导模式内容延迟问题,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我制作了一个模式,用于从WordPress REST API加载一些ajax提供的内容。 我需要在内容关闭时清理它,这是因为我使用相同的代码从不同类型的网站上加载不同的内容。 我注意到,如果我点击一个不同的链接来打开模态,并且内容是使用ajax获取的,那么内容的更新会有一点延迟,这将导致用户看到附加到模态体的以前的内容。有没有一个解决办法,我可以申请,以防止这种beaviour? 以下是我正在使用的代码: const modal = $('#content-modal'); const mo

我制作了一个模式,用于从WordPress REST API加载一些ajax提供的内容。 我需要在内容关闭时清理它,这是因为我使用相同的代码从不同类型的网站上加载不同的内容。 我注意到,如果我点击一个不同的链接来打开模态,并且内容是使用ajax获取的,那么内容的更新会有一点延迟,这将导致用户看到附加到模态体的以前的内容。有没有一个解决办法,我可以申请,以防止这种beaviour? 以下是我正在使用的代码:

const modal = $('#content-modal');
  const modalBody = $('.modal-body');
  const modalTitle = $('.modal-title');

  $('.service').on('click', function(e){
    e.preventDefault();
    var id = $(this).attr('data-id');
    var type = $(this).attr('data-type');
    $.getJSON('https://localhost/wordpress/wp-json/wp/v2/'+type+'/'+id, function(data){
      modalTitle.html(data.title.rendered);
      modalBody.html(data.content.rendered);
    });
  });

  $('.staff').on('click', function(e){
    e.preventDefault();
    var id = $(this).attr('data-id');
    var type = $(this).attr('data-type');
    $.getJSON('https://localhost/wordpress/wp-json/wp/v2/'+type+'/'+id, function(data){
      modalTitle.html(data.title.rendered);
      modalBody.html(data.content.rendered);
    });
  });

  modal.on('hidden.bs.modal', function(e){
    e.preventDefault();
    $(this).modal('dispose');
  });

在这种情况下,如果您在事件“show.bs.modal”中清理内容,效果会更好。这样,表单在加载前将是干净的,例如:

const modal = $('#content-modal');
const modalBody = $('.modal-body');
const modalTitle = $('.modal-title');

$('.service').on('click', function(e){
   e.preventDefault();
   var id = $(this).attr('data-id');
   var type = $(this).attr('data-type');
   $.getJSON('https://localhost/wordpress/wp-json/wp/v2/'+type+'/'+id, function(data){
      modalTitle.html(data.title.rendered);
      modalBody.html(data.content.rendered);
    });
  });

  $('.staff').on('click', function(e){
    e.preventDefault();
    var id = $(this).attr('data-id');
    var type = $(this).attr('data-type');
    $.getJSON('https://localhost/wordpress/wp-json/wp/v2/'+type+'/'+id, function(data){
      modalTitle.html(data.title.rendered);
      modalBody.html(data.content.rendered);
    });
  });

  modal.on('hidden.bs.modal', function(e){
    e.preventDefault();
    $(this).modal('dispose');
  });
//再加上这个//

  modal.on('show.bs.modal', function(e){
    modalTitle.html("");
    modalBody.html("");
  });

谢谢你的建议。我听了你的建议,但我不明白为什么引导的dispose事件没有触发以清空模式内容,所以我应用了
hidden.bs.modal
你关于modalBody和ModalTile的建议,现在它们都像一个符咒一样工作。