感谢this answer ,我有一种在页面加载时启动 fancybox iframe 的方法:http://www.casedasole.it/fancybox/A.html
This answer展示如何在启动 iframe 的页面中打开一个 fancybox iframe,其内容以 javascript 编码:http://www.casedasole.it/fancybox/B.html
是否有某种方法可以让 B 做 A 所做的事情 - 即在页面加载时启动自身?
注意:隐藏的 div 方法 ( see here ) 不起作用,因为我需要能够在 iframe 打开后进行导航。
最佳答案
就这样做:
var myContent = "..."; // html as option "B"
jQuery(document).ready(function ($) {
$.fancybox({
// API options as "B"
// build the iframe
content: '<iframe id="myFrame" class="fancybox-iframe" frameborder="0" vspace="0" hspace="0" src="about:blank"></iframe>',
afterShow: function () {
var oIframe = document.getElementById('myFrame');
var iframeDoc = (oIframe.contentWindow.document || oIframe.contentDocument);
iframeDoc.open();
iframeDoc.write(myContent);
iframeDoc.close();
}
});
}); // ready
参见JSFIDDLE
注意myContent
变量包含完整 html
结构,包括DOCTYPE
, <html>
, <head>
一个<body>
部分。虽然这不是强制性的,但它可以让您更好地控制初始 (iframed) 页面的设置。
另请注意,在我的 jsfiddle 中,出于整洁目的,我将基本内容(html
基本结构)与自定义内容(如您的示例中的 <div>
)分开了;)
引用资料:
关于javascript - Fancybox iframe 的内容在启动页面上用 js 编码 - 如何在页面加载时打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26673658/