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

2023-04-19前端开发问题
11

本文介绍了页面加载时立即显示 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 对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui tree树组件怎么自定义添加图标
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26 前端开发问题
493

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5