Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

jquery - FancyBox iframe returns parent.$ as undefined (using WordPress)

I'm trying to close FancyBox from within the iframe, but parent.$ is always undefined. This is my 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>

Replacing parent.$.fancybox.close(); with alert('clicked'); works just fine. I don't understand why parent.$ is undefined when the iframe is in the same domain.

I'm using WordPress 2.9.1, with the FancyBox for Wordpress plugin.

  • main page: //server.local/web/test/index.php
  • iframe page: //server.local/web/test/wp-content/plugins/wp-test/test.htm

The first of these urls is the main page, the second is the iframe page; server.local is my home test server.

Any ideas? I can pastebin the entire source if it would be helpful.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is undefined because WordPress runs jQuery in noConflict mode. Use this instead:

parent.jQuery.fancybox.close();

noConflict mode means $ does not equal jQuery. You have to explicitly use jQuery to access what you normally can access with $.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...