1

在一个网站中,我有一个 iframe,其中包含一个在启动时全屏显示的 photoswipe 应用程序。现在,应该从 iframe 外部控制 photoswipe 幻灯片。我在主站点中有这段代码:

<iframe src="test.html" id="iframe" width="1440" height="1080" name="iframe" scrolling="no" frameBorder="0" ></iframe>
<div id="ruck" ><a href="#" onClick="document.getElementById('iframe').contentWindow.previous();"><img src="img/rueck.png" /></a></div>
<div id="vor" ><a href="#" onClick="document.getElementById('iframe').contentWindow.next();"><img src="img/vor.png" /></a></div>

和 iframe 中的这段代码:

<html>
<head>
    <link href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" rel="stylesheet" />
    <link href="photoswipe.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="klass.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script>
    <script type="text/javascript" src="code.photoswipe.jquery-3.0.4.min.js"></script>
    <script type="text/javascript">
        (function(window, $, PhotoSwipe)
        {
            $(document).ready(function()
            {
                 var myPhotoSwipe = $("#gallery a").photoSwipe(
                {
                    enableMouseWheel: false,
                    enableKeyboard: false,
                    captionAndToolbarHide: false
                });

                $("#gallery a:first").click();
            });
        }
        (window, window.jQuery, window.Code.PhotoSwipe));

function previous()
{
myPhotoSwipe.previous(); 
}

function next()
{
myPhotoSwipe.next(); 
}       
</script>
</head>
<body>
<div id="gallery">      
            <a href="demo/image001.jpg" rel="external"></a>
            <a href="demo/image002.jpg" rel="external"></a>
            <a href="demo/image003.jpg" rel="external"></a> 
        </div>
</body>
</html>

在这里,单击下一个、上一个按钮时出现错误: ReferenceError: myPhotoSwipe is not defined myPhotoSwipe.next();

如您所见,事件正在触发,但 myPhotoSwipe.next() 函数未定义。任何想法,运行代码要改变什么?

提前致谢。

4

1 回答 1

0

我会说你有一个范围问题:

var myPhotoSwipe; //set here to reference it on global scope
(function(window, $, PhotoSwipe)
        {
            $(document).ready(function()
            {
                 myPhotoSwipe = $("#gallery a").photoSwipe(
                {
                    enableMouseWheel: false,
                    enableKeyboard: false,
                    captionAndToolbarHide: false
                });

                $("#gallery a:first").click();
            });
        }
        (window, window.jQuery, window.Code.PhotoSwipe));
于 2013-05-13T13:35:54.440 回答