setInterval 在 IE 中不起作用,使用 document.write 后 Edge



我用ajax调用加载整个页面html,用document.write显示它,然后我做一些修改。我需要使用 document.write 以正确的顺序加载元素,就像通过访问页面加载元素一样。一切正常,除了在IE和Edge上setInterval不起作用。

我的代码:

var my_module = {  
  loadPage: function(){                                                        
      var url = window.location.origin;    
      $.ajax({                                   
         type: "GET",                           
         url: url                                     
       }).done(function(data) {
            document.write(data);                
            document.close();
            my_module.example()  
      });                                       
  },  
  example: function(){
    alert(1) //this works fine
    var waitForElement = setInterval(function(){
      alert(5) //this won't work in IE or edge
    },2000) 
  },                                            
}      
my_module.loadPage();

在其他浏览器中,我会收到警报,但在IE和Edge中没有。这是什么原因,有什么解决方案吗?

我认为

这可能是由于在不同浏览器中执行document.write()的方式不同。您可以查看此文档。另请查看此线程以获取更多信息。如果要在页面中加载某些元素,可以使用appendChild()作为解决方法。

最新更新