从iframe调用父级中的Fancybox

时间:2012-01-13 16:21:33

标签: jquery html iframe fancybox

我有2页,我的index.html和social.html。我清理了我的索引,只留下了fancybox的jquery代码需要我想要做的是在social.html中有图像,当点击它时,它会打开fancybox并显示它但是在index.html中不在iframe内。出现的错误是:

Uncaught TypeError: Property 'showImage' of object [object DOMWindow] is not a function
(anonymous function)social.html:103
  onclick 

这是我的index.html:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.fancybox-1.3.4.css" media="screen" />
<script type="text/javascript" src="js/video.js"></script>
<script type="text/javascript">
        function showImage(image) {
            $.fancybox('<img src="img/' + image + '" alt="" border="0" />');
        };
</script>

</head>

<body>
<iframe src="social.html" scrolling="no" border="0" width="579" height="505" allowtransparency="true"></iframe>
</body>
</html>
在IFrame页面中

 <img src="img/picture1.jpg" alt="" class="thumbs" onclick="parent.showImage('picture1.jpg');"/>

PS:这两个页面都在同一个域上...... 编辑:video.js - &gt;这是来自fancybox我没有做到这一点。

jQuery(document).ready(function() {

    $(".video").click(function() {
        $.fancybox({
            'padding'       : 0,
            'autoScale'     : false,
            'transitionIn'  : 'none',
            'transitionOut' : 'none',
            'title'         : this.title,
            'width'         : 640,
            'height'        : 385,
            'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type'          : 'swf',
            'swf'           : {
            'wmode'             : 'transparent',
            'allowfullscreen'   : 'true'
            }
        });

        return false;
    });



});

3 个答案:

答案 0 :(得分:20)

首先,您应该使用fancybox v2.x无缝地执行此操作(修补fancybox v1.3.x不值得付出努力)

其次,你需要在两个页面,父页面和iframed页面,加载jquery和fancybox css和js文件,以便正确地超越fancybox,

所以在这两个页面中你应该至少有类似的东西:

<link rel="stylesheet" type="text/css" href="fancybox2.0.4/jquery.fancybox.css" />

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="fancybox2.0.4/jquery.fancybox.js"></script>

然后在您的iframed(子)页面中,您的脚本基本相同(但使用v2.x的正确的fancybox选项... check the documentation here

$(".video").click(function() {
        $.fancybox({
            'padding'       : 0,
            // more options (v2.x) etc

但不是这个

        $.fancybox({

这样做:

        parent.$.fancybox({

然后fancybox将在iframed页面边界的父页面中显示

更新Demo page here

答案 1 :(得分:1)

由于img在iframe上是一个不同的网页。因此Jquery在index.html上,但img没有,这就是为什么JQuery无法获得图像。

也许使用这样的东西......但我认为它不会起作用

在index.html脚本部分

$(document).ready(function () {
    //function to active fancybox on the thumbs class inside the iframe
    //Not sure it works on iframes, probably it doesnt.
    $("#iframeID.thumbs").fancybox();
});

并将id添加到iframe声明中     &LT; iframe src = \“social.html \”id =“iframeID”等...其余属性&gt;

但是,我的建议是使用div而不是iframe。 您还应该从图像中删除onclick以获取非阻碍性的Javascript。

查看教程:http://fancybox.net/howto 如果你想要一个西班牙语的类似教程,你也可以查看我的网站。

答案 2 :(得分:1)

我之前遇到过这个问题,并且能够找到解决方案。这是iv做了什么。 在我的iframe页面中,我使用href调用父窗口,例如 onClick="callMe('www.google.com')"或者如果pics“/images/pics.png” 并在我的父页面中调用此脚本

<script type="text/javascript"> 
 function callMe(site){ 
      $(".sample").fancybox({ 
         'width' : '97%', 
         'height' : '97%', 
         'href' : ''+site+'' //force href to change its site from the call function in the iframe page 
      }); 
      readyFancy() call to trigger the decoy link page 
 } 

 function readyFancy(){ 
      $("a.sample").trigger("click"); 
 } 
 </script> 

在我的父html中我们右边有一个诱饵链接(这个是来自加载fancybox的例子)

<body> 
<a href="#" class="sample"></a> 
</body> 

你可以在这里下载一个工作示例.. https://docs.google.com/file/d/0B1TI7BdxGAbvYzBiZjNiMDYtNjY4ZS00NDczLWE2YjQtODNlMjU4YTNjYWI0/edit?authkey=CP_H9rAC

相关问题