【问题标题】:Fancybox 2.1.3 indirectly called from iframe returns 'The requested content cannot be loaded. Please try again later.'从 iframe 间接调用的 Fancybox 2.1.3 返回“无法加载请求的内容。请稍后再试。'
【发布时间】:2012-12-11 21:23:45
【问题描述】:

我正在努力设置最新的fancybox。我设法从 iframe 调用 fancybox 以在主页上打开,但它似乎没有找到内容。

所以,我有以下设置:

主页: 打开 iframe 并包含用于在 fancybox 中显示图像的 javascript 函数。 出于测试目的,我还没有使用任何参数调用该函数,而是使用硬编码的图像,直到我让它工作为止。所以javascript函数看起来像这样:

$jj = jQuery.noConflict();
function triggerFancybox(){
$jj(".triggerid").trigger("click");
alert("Fancybox triggered!");}
function callMe(){
$jj(".triggerid").fancybox('images/showroom/playground/displaced/dispm_6.jpg' , {'type' : 'image'});
triggerFancybox();}
function callMe1(){
$jj(".triggerid").fancybox('http://demo.artonbit.com/images/showroom/playground/displaced/dispm_6.jpg' , {'type' : 'image'});
triggerFancybox();}
function callMe2(){
$jj(".triggerid").fancybox('http://demo.artonbit.com/images/showroom/playground/displaced/dispm_6.jpg');
triggerFancybox();}

主页还包含一个虚拟元素,我用它来打开fancybox内容:

<a href="#" class="triggerid"></a>      <!-- This id can be called by an iframe for the fancybox> 

iframe: iframe 目前只包含 onclick 触发器,以下是最新尝试:

<p><a class="fancyframe" rel="group" href="images/showroom/playground/displaced/dispm_6.jpg" onclick="parent.callMe();return false;"><img style="float: left;" alt="PXM Commercial Shot1" src="images/showroom/playground/displaced/dispm_6.jpg" height="135" width="240" /> </a>
</p>
<p><a class="fancyframe" rel="group" href="images/showroom/playground/displaced/Disp_7b1.jpg" onclick="parent.callMe1();return false;"><img style="float: left;" alt="PXM Commercial Shot1" src="images/showroom/playground/displaced/dispm_6.jpg" height="135" width="240" /> </a>
</p>
<p><a class="fancyframe" rel="group" href="images/showroom/playground/displaced/dispm_9.jpg" onclick="parent.callMe2();return false;"><img style="float: left;" alt="PXM Commercial Shot1" src="images/showroom/playground/displaced/dispm_6.jpg" height="135" width="240" /> </a>
</p>
<p><a href="#" onclick="parent.callMe();return false;">Another Test</a>

所以,fancybox 被触发,但它找不到内容。我尝试了许多不同的调用fancybox的方法,但我总是得到标题中的错误消息。

图片参考是正确的。应包括所有必要的库。我以 id 作为目标对此进行了测试,目前我正在使用一个类(如 Fancybox FAQ 中所建议的那样)。

我错过了什么?

我首先使用较旧的 fancybox 1.3.4 实现了这一点,它运行良好。

您可以在此地址找到问题的运行演示: http://demo.artonbit.com

进入首页后,只需点击最左上角的图块(Displaced!),这将打开一个包含一些文本和三个缩略图的 iframe。这些是fancybox的触发器。他们成功调用了“callMe”方法,但是fancybox 找不到内容。

【问题讨论】:

  • 您只需要一个 jQuery 实例(最好是最新版本)。您正在加载 jQuery v1.8.2 和 v1.7.2
  • 好提示,我还没有注意到这一点。我将搜索添加 v1.7.2 条目(在 joomla 2.5 上运行)的组件/模块并尝试将其删除。我想这可能是一个原因。
  • 好的,现在已经清理了 jQuery 命名空间。稍后还有一个额外的 jQuery (1.7.1) 包含,但它使用自己的命名空间(也是我的代码)。不幸的是,结果还是一样。

标签: javascript jquery iframe fancybox


【解决方案1】:

我检查的第一件事是你的网站和你加载的脚本,所以我认为你只需要一个 jQuery 实例,而不是用两个不同的版本和命名空间来复杂化自己......无论如何。

现在你的代码没有正确的格式,因为你或者

1) 将 fancybox 绑定到一个选择器(例如一个类),例如:

$(".selector").fancybox({
  // API options
});

2) 手动调用fancybox

$.fancybox([
  href: 'images/showroom/playground/displaced/dispm_6.jpg'
],{
  // API options
  type: "image"
});

... 但不是:

$(".triggerid").fancybox('images/showroom/playground/displaced/dispm_6.jpg', { 
   // API options
   'type' : 'image'
});

...这是上述两者的混合。

在您的特定情况下,您只需在函数内部将 fancybox 脚本更改为正确的格式,例如:

function callMe(){
  $(".triggerid").fancybox({
      href: 'http://demo.artonbit.com/images/showroom/playground/displaced/dispm_6.jpg'
  });
  triggerFancybox();
}

...或使用您的命名空间$jj

注意:由于href 具有图像扩展名,因此您实际上不需要指定type: "image"

眼见为实......JSFIDDLE

【讨论】:

  • 我将如何使用手动版本?我的最终目标是提供一个图像列表,以便人们可以浏览它们。似乎我需要手动方法。但我不明白,我将如何设置它(因为不再有选择器,它会在哪里打开?)。
  • @André :查看jsfiddle.net/STgGM 以获取手动方法的示例,以便您可以在函数中进行设置。
  • 太好了,再次感谢,我不知道为什么我的 API 会出现这样的问题,但我现在可以正常使用了。不需要选择器。一旦我准备好我的前 15 分,你就会获得投票;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2017-05-03
  • 2017-04-08
相关资源
最近更新 更多