2017-01-23 4 views
0

Ich versuche, mein Fenster in der Basis einen Wert von Taste umleiten, aber ich kann Arbeit"Nachricht": "Uncaught ReferenceError: myFunction ist nicht definiert"?

dies ist mein Code

<!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>

bitte helfen

+0

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

Antwort

3

JQuery-Code erhalten sollte wie folgt sein:

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

Ihr Script-Tag sollte wie folgt sein:

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

script elements ein src Attribut oder Inhalt haben kann, aber nicht beides. Wenn beide vorhanden sind, wird der Inhalt ignoriert (der Inhalt wird als "Skriptdokumentation" und nicht als Code betrachtet).

einen anderen Skript Block für Ihren jQuery-Skript verwenden

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

Sie müssen die Funktion im globalen Rahmen definieren, wie Sie es mit Inline-Click-Handler zugreifen.

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

Allerdings würde ich empfehlen, unaufdringlich Event-Handler anstelle von Inline-Click-Handler verwenden soll.

$(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

bitte Funktion auf Seite bereit unter Code aufrufen. bitte überprüfen Sie es einmal:

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

$(document).ready(function(){ 
    myFunction(); 
}); 
</script> 
Verwandte Themen