2013-10-15 67 views
0

基本上,我有2个php页面,这是a.php和b.php。我想要实现的是将一个jquery函数绑定到b.php的内容中。绑定到iframe中的内容控制

jquery函数将用于触发a.php中的弹出窗口。但是,我无法得到它的工作。有人知道我做了什么错误吗?

a.php只会

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 
     <script> 
     $(function() { 
     var abc = function(){ 
     alert(""); 
     } 
     $("iframe").contents().find("a.toggle").click(abc); 
     }); 
     </script> 
     <meta http-equiv="cache-control" content="no-cache"> 
     <meta http-equiv="pragma" content="no-cache"> 
    </head> 
    <body> 
     <form method="post" action=""> 
     <div class="container" id=""> 
      <div class="resize main" id="" style="height: 600px;"> 
       <iframe id="" frameborder="0" scrolling="no" src="b.php" style="width: 1022px; border: none; height: 600px;"> 
       </iframe> 
      </div> 
     </div> 
     </form> 
    </body> 
</html> 

b.php

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta http-equiv="cache-control" content="no-cache"> 
     <meta http-equiv="pragma" content="no-cache"> 
    </head> 
    <body> 
     <form method="post" action="b.php"> 
     <div class="titlewrap"> 
      <div id="" class="pagetitle"> 
       <a class="toggle" href="#"><span class="icon slider-close">aa</span></a> 
      </div> 
     </div> 
     </form> 
    </body> 
</html> 
+0

看看这个:http://stackoverflow.com/questions/1655862/attach-click-handler-even-on-element-inside-iframe-with -jquery。另外你的问题与PHP无关。 –

回答

0

我相信这是因为iframe中加载之前正在执行的JavaScript函数。如果更改

$("iframe").contents().find("a.toggle").click(abc); 

setTimeout(function(){ $("iframe").contents().find("a.toggle").click(abc); }, 2000); 

那么你的代码工作。

因此,要解决您的问题,我会建议将绑定函数移动到b.php并在document.ready事件触发时调用它。

b.php

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta http-equiv="cache-control" content="no-cache"> 
     <meta http-equiv="pragma" content="no-cache"> 
     <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 
     <script> 
     $(document).ready(
     function() 
     { 
      var abc = function(){ 
      alert(""); 
      } 
      $('.toggle').on('click', abc); 
     } 
     ); 
     </script> 
    </head> 
    <body> 
     <form method="post" action="b.php"> 
     <div class="titlewrap"> 
      <div id="" class="pagetitle"> 
       <a class="toggle" href="#"><span class="icon slider-close">aa</span></a> 
      </div> 
     </div> 
     </form> 
    </body> 
</html>