简体   繁体   English

在DOM准备就绪之前使用jQuery加载函数

[英]Using jQuery Load Function before DOM Ready

I have a page that renders HTML blocks from another page on the same domain using IDs. 我有一个页面,该页面使用ID呈现来自同一域中另一个页面的HTML块。 My current code: 我当前的代码:

<div id=”testdiv”></div>
<script>
jQuery(document).ready(function(){ 
   jQuery('#testdiv').load('/references/embed1.html #testdiv2'); 
});
</script>

While this loads the content correctly, there is a visible lag between the page loading and the jQuery content loading; 在正确加载内容的同时,页面加载和jQuery内容加载之间存在明显的滞后。 depending on the DIV contents it sometimes a full second to display then it just pops into place. 根据DIV的内容,有时会显示一整秒,然后弹出到位。 This is obviously due to the page not attempting to retrieve the HTML content until DOM Ready so I removed the ready function but the Load function doesn't run. 这显然是由于页面直到DOM Ready才尝试检索HTML内容,因此我删除了ready函数,但是Load函数没有运行。 If I use an iFrame instead it appears to load as the browser executes the code but I lose the ability to only include specific DIV IDs and it's difficult to make it responsive. 如果我使用的是iFrame,它似乎是在浏览器执行代码时加载的,但是我失去了仅包含特定DIV ID的功能,因此很难做出响应。 Looked at $.ajax but apparently Load uses .ajax so it doesn't look like there will be a difference. 看了$ .ajax,但显然Load使用了.ajax,因此看起来没有什么区别。

Simply put – how do I load specific DIV ids from another page without waiting for DOM Ready whether jQuery, JavaScript, iFrames or other method? 简而言之–如何在不等待DOM就绪的情况下从另一页加载特定的DIV ID,无论是jQuery,JavaScript,iFrame还是其他方法? Second question 第二个问题

Thanks 谢谢

document ready will be triggered until the whole page were loaded, just remove it and .load() will be invoked after #testdiv had finish render on DOM. document ready将被触发,直到整个页面被加载为止,只需将其删除,并在#testdiv在DOM上完成渲染后将调用#testdiv .load() here's the example 这是例子

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<div id="testdiv"></div>
<div id="error"></div>

<script>
$( "#testdiv" ).load( "/references/embed1.html #testdiv2", function( response, status, xhr ) {
  alert("Triggered");
  if ( status == "error" ) {
    var msg = "Err trying to load ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
  }
});
</script>

https://jsfiddle.net/Angel_xMu/rer3yuny/1/ https://jsfiddle.net/Angel_xMu/rer3yuny/1/

Ajax is not instant, and nothing you do will change that. Ajax不是即时的,您所做的任何更改都不会改变。 Therefore, there will always be some form of delay. 因此,总会有某种形式的延迟。 You can reduce the delay by removing the need for $(document).ready() , however I suspect it still won't be enough to have it do what you were hoping for. 您可以通过消除对$(document).ready()的需要来减少延迟,但是我怀疑$(document).ready()它来做您想要的还是不够的。

$.get('page.html', function (result) {
  // note, you may need to use `.filter` rather than `.find` depending on the structure of `result`
  $('#target').html($($.parseHTML(result)).find('#target2'));
});

or leave your code as is minus $(document).ready and move it to after the target div just like in your example. 或将您的代码保留为减去$(document).ready并将其移动到目标div之后,就像您的示例一样。

The only way to completely remove the delay would be to remove the need for using $.ajax by inserting the html directly into the page server-side. 完全消除延迟的唯一方法是通过将html直接插入页面服务器端来消除使用$.ajax的需要。

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

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