2017-01-23 12 views
0

私がベースで、ボタンの値を私の窓をリダイレクトしようとしているが、私は仕事"message": "Uncaught ReferenceError:myFunctionが定義されていません"?

を得ることができ、これは私のコード

<!DOCTYPE html> 
 
<html> 
 
<body> 
 
    <p>Click the button to change the location of the first iframe element (index 0).</p> 
 

 
    <button class="godd" onclick="myFunction()">Try it</button> 
 
    <br> 
 
    <br> 
 

 
    <iframe src="http://localhost:8005/?xform=http://127.0.0.1:8080/output_path4fff"></iframe> 
 
    <iframe src="http://localhost:8005/?xform=http://127.0.0.1:8080/output_path4fff"></iframe> 
 

 
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"> 
 
    $(document).ready(myFunction() { 
 
     var Vp = $('.godd').eq(0).text(); 
 
     window.frames[0].top.location.href = 'http://' + Vp; 
 
    }) 
 
    </script> 
 
</body> 
 
</html>

ある

+0

checkout this http://stackoverflow.com/questions/15171008/onclick-not-working-with-button – ricky

答えて

3

jQueryのコードを助けてください次のようにする必要があります:

function myFunction() { /* myfunction declaration */ 
    var Vp = $('.godd').eq(0).text(); 
    window.frames[0].top.location.href = 'http://' + Vp ; 
}; 
$(document).ready(function(){ /* DOM ready callback */ 

}); 

You should define your function outside DOM ready. Read $(document).ready

あなたのスクリプトタグは次のようにする必要があります:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script> 
    //Jquery Code 
</script> 
2

script elementssrc属性またはコンテンツを持つことができ、両方はできません。両者がある場合、コンテンツは無視されます(コンテンツはコードではなく「スクリプトのドキュメント」と見なされます)。

はあなたがインラインクリックハンドラを使用してアクセスすると、グローバルスコープで関数を定義する必要があなたのjQueryのスクリプト

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
</script> 
<script> 
    function myFunction() { 
    } 
</script> 

のための別のスクリプトブロックを使用してください。控えめなイベントハンドラの代わりに、インラインクリックハンドラを使用する必要があり、私が推薦するしかし

function myFunction() { 
    var Vp = $('.godd').eq(0).text(); 
    window.frames[0].top.location.href = 'http://' + Vp ; 
}; 
$(document).ready(function(){ 
    // DOM ready callback   
}); 

$(document).ready(function(){ 
    $('.godd').on('click', function() { 
     var Vp = $(this).text(); //Current element context 
     window.frames[0].top.location.href = 'http://' + Vp ; 
    }); 
}); 

HTML

<button class="godd">Try it</button> 
2

コードの下準備ページに関数を呼び出してください。一度確認してください:

<script> 
function myFunction(){ 
     var Vp = $('.godd').eq(0).text(); 
     window.frames[0].top.location.href = 'http://' + Vp ; 
} 

$(document).ready(function(){ 
    myFunction(); 
}); 
</script> 
関連する問題