簡體   English   中英

在通過Fancybox彈出窗口打開的iFrame中調用javascript函數

[英]Calling a javascript function in iFrame opened with Fancybox popup

我正在嘗試使用jQuery Fancybox在iFrame中打開的頁面中調用javascript函數。

            $(".testbox").fancybox({
                maxWidth    : 800,
                maxHeight   : 600,
                fitToView   : false,
                autoSize    : false,
                closeClick  : false,
                openEffect  : 'none',
                closeEffect : 'none',
                afterShow: function() {
                        //var frameID = $('#fancybox-frame')
                        //frameID.getParam("test");                         
                    }
            }); 

        $(".testbox").each(function() {
                var element = this;
                $(this).fancybox({
                    'titleFormat'   : function() {
                        var astring = '<span>' + element.id + '</span>';
                        alert(astring);
                    }
                });
            });

HTML:

    <a class="testbox fancybox.iframe" href="include/testFrame.html">Iframe</a>

我正在嘗試調用testFrame.html中的函數:

        function getParam(param) {              
            alert(param);               
        }

通常,在不使用Fancybox彈出窗口的情況下在iFrame中調用函數時,我會這樣做:

        document.getElementById('myFrame').contentWindow.getParam();

但我不知道在使用Fancybox時如何獲取整個iFrame的ID。 任何幫助表示贊賞!

編輯:我也嘗試了此代碼也無法正常工作,我得到IE中的錯誤:對象不支持此屬性或方法。 我確定我要加載的頁面具有功能

    var $f = $("#fancybox-frame");
    var fd = $f[0].document || $f[0].contentWindow.document; 
    fd.getparam("test");  

您應該使用:

var $f = $("#fancybox-frame");
$f[0].contentWindow.getParam("test");

原因:

  • 該方法是全局聲明的,而不是作為document對象的方法聲明的。
  • JavaScript區分大小寫; getParam是從不同getparam

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM