2017-01-23 1 views
0

내가베이스 버튼의 값을 내 창을 재려고하지만 난 직장 얻을 수는"message": "잡히지 않은 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 속성 또는 내용을 가질 수 있지만 둘. 양쪽 모두가 있으면 내용은 무시됩니다 (내용은 코드가 아닌 "스크립트 설명서"로 간주됩니다).

는 인라인 클릭 핸들러를 사용하여 접근과 같이 글로벌 범위에서 함수를 정의 할 필요가

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

당신의 jQuery 스크립트 다른 스크립트 블록을 사용합니다.

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> 
관련 문제