SimpleModal 在对话框中加载外部 HTML 页面
是否可以将外部 HTML 文件加载到变量中,然后使用该变量加载 SimpleModal 对话框?像这样的事情:
$(document).ready(function($) {
var externalPage $.get("Renderer.htm");
$('#basic-modal .basic').click(function(e) {
$(externalPage).modal();
return false;
});
});
另一种解决方案(可行)是在隐藏的 div 中加载外部 HTML 文件,然后使用它来加载对话框。
$(document).ready(function($) {
$('#simplemodal-content').hide(); // or hide in css
$('#simplemodal-content').load("Renderer.htm");
$('#basic-modal .basic').click(function(e) {
$('#simplemodal-content').modal();
return false;
});
});
但是,如果我采用这种方法,为外部页面定义的 CSS 可能会干扰我的本地页面,从而更改布局,这是不希望的。
如果有比这些方法更好的第三种解决方案,请分享。
PS:遗憾的是它也必须在 IE6 中完美运行。
Is it possible to load an external HTML file into a variable and then use this variable to load the SimpleModal dialog? Something like this:
$(document).ready(function($) {
var externalPage $.get("Renderer.htm");
$('#basic-modal .basic').click(function(e) {
$(externalPage).modal();
return false;
});
});
An alternative solution (that works) is loading the external HTML file in a hidden div and then use this to load the dialog.
$(document).ready(function($) {
$('#simplemodal-content').hide(); // or hide in css
$('#simplemodal-content').load("Renderer.htm");
$('#basic-modal .basic').click(function(e) {
$('#simplemodal-content').modal();
return false;
});
});
However if I take this approach the css defined for the external page can interfere with my local page and thus change the layout, which is not desired.
If there's a 3rd solution that's better than these approaches, please share.
PS: sadly it also has to work perfectly in IE6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为您需要使用 iframe 来完成此任务。即使您能够在页面上显示页面内容后将其加载到变量中,它的 css 和 javascript 也会开始影响您的页面。
像这样的东西可能会起作用:
虽然您可能不想直接使用 iframe 作为模式对象,但我希望这足以让您指向正确的方向。
I think you will need to use an iframe to accomplish this. Even if you are able to load the contents of the page into a variable once you display it on the page, its css and javascript will start affecting your page.
Something like this might work:
Though you will probably not want to use the iframe directly as modal object, but I hope that this is enough to get you pointed in the right direction.
不要加载整个 html 文件,而只加载其中的一部分。
这将仅加载正文,不包括任何 CSS 或脚本。
Rather than loading the entire html file, load just one part of it.
This will load the body only, excluding any CSS or scripts.
Keare 的答案确实有助于将 css/js 与外部 html 分开,这样它就不会干扰当前页面。它也可以用作模式对话框的基础。
作为替代方案,我还发现了这个解决方案,它在模式对话框本身中使用 iframe。在这种情况下,您可能会遇到滚动条问题,该问题已在此处解决:http://bit.ly/anbyf2
The answer of Keare is indeed useful to seperate the css/js from the external html so it doesn't interfere with the current page. It can also be used as a base for the modal dialog.
As an alternative I've also found this solution which uses an iframe in the modal dialog itself. In this case you might have a problem with scrollbars, which is already solved here: http://bit.ly/anbyf2
虽然滚动您自己的解决方案非常适合学习,并且可能会更有效,但有大量 jQuery 插件可以执行此操作(并已为您解决了所有问题)。示例包括:
http://colorpowered.com/colorbox/ 和 http://fancybox.net/
Whilst rolling your own solution is great for learning, and may possibly be more efficient, there are plenty of jQuery plug-ins that do this (and have solved all the problems for you). Examples include:
http://colorpowered.com/colorbox/ and http://fancybox.net/
SimpleModal 网站上的 评论 表明以下代码应该有效:
A comment on the SimpleModal site indicates that the following code should work: