2016-07-18 2 views
0

아래 제가 다른 자바 스크립트 함수 정규 자바 스크립트 함수를 호출하는 방법을 지금 키를 사용하여 오디오를 제어하는 ​​데 사용되는 자바 스크립트 함수이다어떻게 다른 스크립트 하나의 자바 스크립트를 호출

var editor = document.getElementById("CKEditor1"); 
editor.on('contentDom', function() { 
    editor.document.on('keydown', function (event) { 
     if (event.data.key == 32) 
      alert('space bar pressed'); 
    }); 
}); 
+0

CK 편집기의 코드는, 익명 함수입니다 :

그래서 당신은 여기에 코드를 배치해야합니다 그것은 전역 객체에 있습니다. 하지만 그것은 이벤트 핸들러이므로 호출되지 않아야하지만 이벤트가 트리거 될 때입니다. 두 번째 스 니펫에서 함수 호출. script 태그가 동일한 문서에 있고 head 섹션에 배치되면 함수를 이름으로 액세스해야합니다. 그러나 당신이 찾고있는 것을 더 잘 설명하십시오. –

+0

페이지에서 2 단 발췌 부분이 어떻게 나타나는지, 그리고 사용하려고 할 때 무엇을 얻었는지 설명하십시오. –

+0

왜 '느리게'와 같은 기능을 호출 할 수 없었습니까? 오류있어? 아무것도 없어? –

답변

0
var editor = document.getElementById("CKEditor1"); 
editor.on('contentDom', function() { 
    editor.document.on('keydown', function (event) { 
     if (event.data.key == 32) 
      alert('space bar pressed'); 
}); 
}); 
+0

질문을 변경해야하며 답변이 없으면 답장을 보내지 마십시오. –

0

당신은 추적의 예로서 직접 함수를 호출해야합니다

<script type = "text/javascript">//final 
     $(document).ready(function() { 
      CKEDITOR.on('instanceCreated', function (e) { 
       e.editor.on('contentDom', function() { 
        e.editor.document.on('keydown', function (event) { 
         var x = event.data.$.keyCode; 
         if (x == 32) { 
          alert('spacebar is pressed'); 
          return true; 
         } else if (x == 115) { 
          if (audio.paused) { 
           audio.play();//f4 
          } else { 
           audio.pause() 
          } 
         } else if (x == 119) { 
          // Log before the function 
          console.log("calling Forward()"); 
          Forward();//F8 
          // Log after the function 
          console.log("after Forward()"); 
          } else if (x == 120) { 
          // Log before the function 
          console.log("calling Slow()"); 
          Slow();//F9 
          // Log after the function 
          console.log("after Slow()"); 
          } 

         var count = 0; 

        }); 
       }); 
      }); 

     }); 

</script> 

을 다른 기능을 위해 당신이 유사한 작업을 수행해야합니다. 당신이 '를 keyDown'이벤트를 바인딩으로

, 당신은 당신의 두 번째 코드의 부분을 고려해야한다 : 댓글이 말한대로

window.addEventListener("keydown", function (e) { 
    // space, page up, page down and arrow keys: 
    if ([32, 33, 34, 37, 38, 39, 40].indexOf(e.keyCode) > -1) { 
     e.preventDefault(); 
    } 
}, false); 

여기에 키 코드가 화살표입니다을하고 또한 당신이에서 easely 확인할 수 있습니다 site. 당신이이 함수에 이름을 부여 전까지 전화 및 글로벌 범위 또는 맵에 정의 질수 있도록

window.addEventListener("keydown", function (e) { 
    // space, page up, page down and arrow keys: 
    if ([32, 33, 34, 37, 38, 39, 40].indexOf(e.keyCode) > -1) { 

     console.log("You pressed " + e.keyCode); 

     // I assume right arrow for forward 
     if (x == 39) { 
      // Log before the function 
      console.log("calling Forward()"); 
      Forward();//F8 
      // Log after the function 
      console.log("after Forward()"); 
     } else if (x == 37) { // left arrow to slow 
      // Log before the function 
      console.log("calling Slow()"); 
      Slow();//F9 
      // Log after the function 
      console.log("after Slow()"); 
     } 

     // then prevent to continue the other event handlers to avoid scrolling. 
     e.preventDefault(); 
    } 
}, false); 
+0

내가 '천천히()'로했던 것처럼 필요한 곳에서 필요한 기능을 호출하면 올바르게 작동합니다. –

+0

@xyz 코드를 어떻게 변경해야하는지에 대한 예제를 제공하여 답변을 변경했습니다. 희망이 도움이됩니다. –

+0

로그가 표시되지 않으면 코드가 실행되지 않는다는 것을 의미하므로 핸들이 호출되었는지 확인하고 키 코드 값을 확인하십시오. 디버깅하려면 먼저 'if'앞에 로그를 기록하십시오. –

관련 문제