2016-07-21 23 views
0

私はチュートリアルを試してみましたが、まだ動作しませんでした、何が問題なのですか?親ページからiframeの値を取得できません

親ページのスクリプト:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Parent</title> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-3.1.0.slim.js"></script> 
<script> 
$(function() { 
    var money = 20000; 

    $("#iframe1").contents().find("#nominal").on("change keyup", function() { 
     var input = $(this); 

     // remove possible existing message 
     if(input.next().is("form")) 
      input.next().remove(); 

     // show message 
     if(input.val() > money) 
      alert('Hello'); 
    }); 
}); 
</script> 
    </head> 
    <body> 
     <iframe id="iframe1" src="iframe.html"> 
     </iframe> 
    </body> 
</html> 

インラインフレームスクリプト:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Iframe</title> 
    </head> 
    <body> 
     <input type="text" id="nominal" value="" /> 
    </body> 
</html> 

おかげで進め、間違っていたものを私に教えてください。先加载iframe

答えて

0

load完成后再监听click

事件まず負荷iframeclickイベントリスナーの終了後などload等。

はこれを試してみてください。

<!DOCTYPE html> 
    <html> 
    <head> 
     <title>Parent</title> 
     <script src="//cdn.bootcss.com/jquery/2.2.4/jquery.js"></script> 
     <script> 
      $(function() { 

       $("#iframe1").attr("src","iframe.html"); 

       //iframe加载完毕以后执行的事件 
       $("#iframe1").load(function(){ 
        console.log('loaded...'); 
        Handle(); 
       }) 

      }); 

      function Handle(){ 
       var money = 20000; 
       $("#iframe1").contents().find("#nominal").on("change keyup", function() { 
        var input = $(this); 
        // remove possible existing message 
        if(input.next().is("form")) 
         input.next().remove(); 
        // show message 
        if(input.val() > money) 
         alert('Hello'); 
       }); 
      } 

     </script> 
    </head> 
    <body> 
    <iframe id="iframe1"> 
    </iframe> 
    </body> 
    </html> 
+0

が表示されない、警告を動作することはできません。ここではコンソール 'jQuery.Deferred exception:$(...)からのエラーログがあります:loadは関数ではありません@http://localhost/iframe/parent.html:12:17 resolve/

+0

jqueryのバージョンを変更し、バージョン2.2では3を使用しません。 –

+0

同じBroは動作しません。' ReferenceError:$ is not defined'エラーコンソールにログイン –

関連する問題