页面加载时立即显示 JQuery 对话框

JQuery dialog momentairly displayed on page load(页面加载时立即显示 JQuery 对话框)
本文介绍了页面加载时立即显示 JQuery 对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用标准的 JQuery UI 功能创建了一个具有基于 JQuery 的对话框的页面.我使用 JQuery 的开箱即用功能来做到这一点……没什么特别的.这是我的对话框 HTML:

I created a page that has a JQuery based dialog using the standard JQuery UI function. I do this with out of the box functionality of JQuery... nothing special at all. Here is my HTML for the dialog:

<div id = "myDialog">
    <!-- ... more html in here for the dialog -->
</div>

然后在 javascript 中调用 JQuery 来转换 <div>到一个对话框:

Then the JQuery called in javascript that transforms the <div> to a dialog:

    // pruned .js as an example of kicking up a JQuery dialog
    $('#myDialog').dialog({
        autoOpen: false,
        title: 'Title here',
        modal: true
        }
    });

再次,简单的 JQuery.因此,您可以通过单击父页面上的链接来启动此向导,然后它会生成一个 JQuery 对话框,其中包含大量 HTML,包括图像等.

Again, plain-vanilla JQuery. So you start this wizard by clicking on a link on the parent page, and it then spawns a JQuery dialog which has a significant chunk of HTML that includes images, etc.

当我继续开发这个页面时,我开始注意到当我在浏览器中加载页面时,<div>我放入的将 JQuery 转换为对话框的标签将非常简短地显示出来.然后页面将按预期运行.换句话说,对话框不会被隐藏,它会在页面中简单地显示出来. 看起来很丑陋和不专业!但片刻之后,页面会正确呈现并且看起来和我预期/想要的一样.

As I continued developing this page, I started to notice that when I loaded the page in the browser that the <div> tags I was putting in that JQuery transforms into dialogs would very briefly be displayed. Then the page would act as expected. In other words, the dialog would not be hidden, it would be displayed briefly in-line in the page. Quite ugly and unprofessional looking! But after a split second, the page would render correctly and look just as I expected/wanted.

随着时间的推移,随着页面大小的增加,页面保持不正确呈现的时间也会增加.我的猜测是浏览器的渲染引擎在页面加载时渲染页面,最后它会启动将转换 <div> 的 JQuery.进入对话框.然后这个 JQuery 函数将转换简单的 <div>到一个 JQuery 对话框并将其隐藏(因为我将 autoOpen 属性设置为 false).部分浏览器<咳嗽>IE</cough>显示它比其他人更长.我的大型对话框现在会导致页面错误地呈现大约 1 秒...太糟糕了!

Over time, as the page size grew, the time the page would remain incorrectly rendered grew. My guess is that the rendering engine of the browser is rendering the page as it is loading, then at the end it is kicking off the JQuery that will transform the <div> into a dialog. This JQuery function will then transform the simple <div> to a JQuery dialog and hide it (since I have the autoOpen property set to false). Some browsers <cough>IE</cough> display it longer than others. My large-ish dialog now causes the page to incorrectly render for about 1 second... YUCK!

推荐答案

您可以添加一些CSS,使其默认隐藏,无需加载代码:

You can add some CSS so it's by default hidden, no onload code needed:

#myDialog { display: none; }

有了这个,就不需要其他代码了,当你打开对话框时,它会反转这种风格......所以在 document.ready 上不需要运行任何额外的代码.或者,如果您有很多对话框,请给它一个类,如下所示:

With this, no other code is necessary, when you open the dialog it'll reverse this style...so nothing additional to run on document.ready. Alternatively, if you have lots of dialogs, give it a class, like this:

<div id="myDialog" class="dialog"></div>

使用这个 CSS:

.dialog { display: none; }

在几乎所有情况下,jQuery 都使用 display 样式属性来隐藏内容,因此使用它最初隐藏某些内容将适用于您以后想要使用该元素的任何内容,无论是对话框,一个简单的.fadeIn()

In almost all cases, jQuery uses the display style attribute to hide things, so using this to initially hide something will work with whatever you want to use the element for later, whether it's a dialog, a simple .fadeIn(), etc.

这篇关于页面加载时立即显示 JQuery 对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
$.ajax call working fine in IE8 and Doesn#39;t work in firefox and chrome browsers($.ajax 调用在 IE8 中运行良好,但在 Firefox 和 chrome 浏览器中不起作用)
How do I know if jQuery has an Ajax request pending?(我如何知道 jQuery 是否有待处理的 Ajax 请求?)
document.write() overwriting the document?(document.write() 覆盖文档?)