2013-10-15 2 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

Я считаю, что это потому, что ваш Javascript функция выполняется прежде, чем загрузится IFrame. Если изменить

$("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> 
Смежные вопросы