FancyBox iframe返回parent.$ as undefined(使用WordPress)

Ale*_*ndr 9 wordpress iframe jquery undefined fancybox

我试图从iframe中关闭FancyBox,但parent.$总是如此undefined.这是我的iframe JavaScript:

 <script type='text/javascript' 
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'>
 </script>
 <script type="text/javascript">
 jQuery(document).ready(function($){
     (function($) {
         $.fn.closeFancyBox = function() {
             $(this).click(function() {
                 parent.$.fancybox.close();
             });
         };
      })(jQuery);
      $('#cancel').closeFancyBox();
      });
 });
 </script>
Run Code Online (Sandbox Code Playgroud)

更换parent.$.fancybox.close();alert('clicked');的作品就好了.我不明白为什么parent.$undefined当iframe是在同一个域中.

我正在使用WordPress 2.9.1和FancyBox for Wordpress插件.

  • 主页: //server.local/web/test/index.php
  • iframe页面: //server.local/web/test/wp-content/plugins/wp-test/test.htm

这些网址中的第一个是主页面,第二个是iframe页面; server.local是我的家庭测试服务器.

有任何想法吗?如果它有用,我可以将整个源代码粘贴.

Dou*_*ner 7

它是未定义的,因为WordPress在noConflict模式下运行jQuery .请改用:

parent.jQuery.fancybox.close();
Run Code Online (Sandbox Code Playgroud)

noConflict模式意味着$不等于jQuery.您必须明确用于jQuery访问通常可以访问的内容$.


小智 5

我的答案与 wordpress 无关,而是与一般的fancybox 相关。

在 iframe 中,如果你包含了主 jquery 脚本(jquery-1.5.2.min.js),那么它将与主页上的脚本冲突,并且 在这种情况下parent.$.fancybox将不起作用。

其他与 jquery 相关的东西(例如:标签)将在 iframe 内工作。因此,新手程序员不会想到 iframe 中的第二个 jquery 脚本是恶棍。


Ale*_*ndr 0

花了好几个小时尝试调试这个但一无所获。所以我用最新版本的 FancyBox 替换了“FancyBox for WordPress”插件,并且它被修复了。真的应该早点尝试一下。

在花了一些时间使用 WordPress 及其各种插件之后,我建议手动调用而不是依赖插件。它只是增加了另一层复杂性,如果您知道自己在做什么,则不需要在那里。

感谢 Doug 指出了 WordPress 中 iframe 到父窗口 jQ​​uery 的适当语法。