jQuery:关于图像预加载的问题
我构建了自己的表单脚本,并希望预加载一个“正在加载”图标,该图标在点击提交按钮时显示。
我正在使用这段代码:
var icon = $('<img />').attr('src', 'images/status.gif');
我的问题: 当使用 jQuery 创建图标的 html 标记时,图像是否会被缓存并立即显示?喜欢:
statusDiv.html("<img src='images/status.gif'> Submitting ..");
谢谢
i built my own form-script and would like to preload a "loading"-icon which shows up when hitting the submit button.
i'm using this code:
var icon = $('<img />').attr('src', 'images/status.gif');
my question:
will the image be cached + show up immidiately also when using jQuery for creating the icon's html-markup? like:
statusDiv.html("<img src='images/status.gif'> Submitting ..");
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,执行此操作时将从缓存中提取图像:) 它不是特指 jQuery,这只是本机浏览器行为,无论您以何种方式创建图像都应该有效。
Opera IIRC 是个例外,默认情况下它不会预加载图像,因为它意识到图像尚未显示,除非在这种情况下,它的行为应该像您想要的那样。
另一种可能情况是图像还没有时间加载,例如,如果我在加载页面后非常快点击提交,但这应该是不太可能的,并且获取图像的请求应该已经在运行。
顺便说一句,如果只是针对此图像,您可以缩短预加载:
Yes, the image will be pulled from cache when doing this :) It's not jQuery specifically, this is just native browser behavior, whatever way you create the image should work.
The exception here is Opera IIRC, it won't preload the image by default since it realizes it's not being displayed yet, barring that case it should behave like you want.
The other possible case is the image hasn't yet had time to load, e.g. if I clicked submit really fast after loading the page, but this should be pretty unlikely, and the request to get the image should already be running.
As an aside, you can shorten the preload if it's just for this image:
也许您可以尝试仅使用 XHTML 将其放置在正确的位置,并使用 CSS 使其不可见。我想它会被加载,然后单击它即可使其可见。
您可以在 firebug 中检查您的网络选项卡。
请让我们知道这有效。
Maybe you could try to place it on the right place, just with XHTML, and make it invisible with CSS. I guess it will be loaded, and on click you make it visible.
You can check your net tab in firebug.
Please let us know of that works.