繁体   English   中英

在后台线程上将HTML加载到Div中?

[英]Load HTML into Div on background thread?

我正在使用以下方法将外部HTML文件加载到DIV中:

    var docName = name + ".html";
    $("#displayPanel").load(docName);

然后我在控制该HTML文件的javascript文件上调用getScript,如下所示:

    docName = name + ".js";
    $.getScript(docName, function( data, textStatus, jqxhr ) {

        if (textStatus==="success") {
            lightItUp(); //load method
        }
    });

它正确加载。

问题是,对于大型HTML / JS文件,DOM被冻结,用户交互被禁用,直到加载完成。 是否可以将div加载到“后台线程”并接收完成通知?

不确定我是否正确理解了你的问题。

如果你想让它好像div在后台线程上加载并在完成时收到一个notif,你可以让div display:none并且使它display:block回调中的display:block

$( "#aDivInDisplayPanel" ).load( docName, function() {
  //load completed.
  $(this).fadeIn();
});

您是否考虑过使用HTML5 Web Workers?

http://www.w3schools.com/html/html5_webworkers.asp

在HTML页面中执行脚本时,页面将变为无响应,直到脚本完成。

Web worker是一种在后台运行的JavaScript,与其他脚本无关,不会影响页面的性能。 您可以继续执行任何操作:在Web工作程序在后台运行时,单击,选择内容等。

除非你以某种方式使AJAX同步,否则上面的代码不能阻止UI,除非浏览器需要很长时间来处理HTML(IE 6.6可能需要1-2分钟来解析以这种方式加载的HTML)或者你在lightItUp()有一些东西lightItUp()这需要很长时间。

下一步是启动浏览器的性能工具并检查时间。 您在上面发布的代码看起来是正确的,不应该导致您描述的内容。

将 HTML 装入<div></div><div id="text_translate"><p><em><strong>小提琴</strong>: <a href="https://jsfiddle.net/x9ghz1ko/" rel="nofollow noreferrer">https://jsfiddle.net/x9ghz1ko/</a> (带评论。)</em></p><p> 我有一个<strong>HTML 文档</strong>,在这个文档里面,有<strong>两个部分</strong>: &lt;section class="desktop_only"&gt;和&lt;section class="mobile_only"&gt; - 在代码中,在这些部分之前,有一个<strong>脚本</strong>,这个脚本应该<strong>删除两个部分之一</strong>,但问题是:为此,我必须将脚本放在部分之后,但我不能这样做,因为部分内有更多脚本,我必须删除它们在他们跑之前。</p><p> 我的代码的简化版本:(……不工作。) </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script&gt; device = "desktop"; // This could be either: "desktop", or: "mobile" if(device === "desktop"){ document.querySelector(".mobile_only").remove(); // Not doing anything. } else { document.querySelector(".desktop_only").remove(); // Not doing anything. } &lt;/script&gt; &lt;section class="desktop_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("desktop_only") &lt;/script&gt; &lt;div&gt; desktop &lt;/div&gt; &lt;/section&gt; &lt;section class="mobile_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("mobile_only") &lt;/script&gt; &lt;div&gt; mobile (Delete this.) &lt;/div&gt; &lt;/section&gt;</pre></div></div><p></p><p> <strong>也许一种解决方案是:</strong>我有一个空的&lt;div&gt; ,称为".platform_dependent" ,我有两个外部文件,称为: desktop_only.something和mobile_only.something然后我将这些文件的内容加载到: platform_dependent.innerHTML ...但我不知道该怎么做?</p></div>

[英]load HTML into <div>

暂无
暂无

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

相关问题 HTML DIV作为另一个DIV的背景 加载html或div,菜单 将 HTML 装入<div></div><div id="text_translate"><p><em><strong>小提琴</strong>: <a href="https://jsfiddle.net/x9ghz1ko/" rel="nofollow noreferrer">https://jsfiddle.net/x9ghz1ko/</a> (带评论。)</em></p><p> 我有一个<strong>HTML 文档</strong>,在这个文档里面,有<strong>两个部分</strong>: &lt;section class="desktop_only"&gt;和&lt;section class="mobile_only"&gt; - 在代码中,在这些部分之前,有一个<strong>脚本</strong>,这个脚本应该<strong>删除两个部分之一</strong>,但问题是:为此,我必须将脚本放在部分之后,但我不能这样做,因为部分内有更多脚本,我必须删除它们在他们跑之前。</p><p> 我的代码的简化版本:(……不工作。) </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script&gt; device = "desktop"; // This could be either: "desktop", or: "mobile" if(device === "desktop"){ document.querySelector(".mobile_only").remove(); // Not doing anything. } else { document.querySelector(".desktop_only").remove(); // Not doing anything. } &lt;/script&gt; &lt;section class="desktop_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("desktop_only") &lt;/script&gt; &lt;div&gt; desktop &lt;/div&gt; &lt;/section&gt; &lt;section class="mobile_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("mobile_only") &lt;/script&gt; &lt;div&gt; mobile (Delete this.) &lt;/div&gt; &lt;/section&gt;</pre></div></div><p></p><p> <strong>也许一种解决方案是:</strong>我有一个空的&lt;div&gt; ,称为".platform_dependent" ,我有两个外部文件,称为: desktop_only.something和mobile_only.something然后我将这些文件的内容加载到: platform_dependent.innerHTML ...但我不知道该怎么做?</p></div> 在div中加载html,然后将其删除 将html链接加载到div中 在页面加载上设置div的背景 在页面加载时偏移Div背景 HTML:具有固定背景的可滚动div 如何在 html 中使用 div 作为背景 如何在html中后台加载图像
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM