【问题标题】:how to call one javascript in another script如何在另一个脚本中调用一个 javascript
【发布时间】:2016-11-21 12:17:24
【问题描述】:

以下是用于使用按键控制音频的 java 脚本函数,现在我如何在另一个 java 脚本函数中调用普通的 java 脚本函数

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

【问题讨论】:

  • ck 编辑器中的代码是一个匿名函数,所以你不能调用它,直到你给这个函数命名并在全局范围内定义或映射到一个全局对象中。但它是一个事件处理程序,所以不应该被调用,而是在事件被触发时调用。关于在第二个 sn-p 中调用该函数。如果脚本标签在同一个文档中并且放在 head 部分,您应该按名称访问函数。但请更好地解释您在寻找什么。
  • 请说明这 2 个 sn-ps 在您的页面中的显示方式以及您尝试使用它时得到的结果。
  • 那么为什么不能调用像'Slow()'这样的函数呢?你有错误吗?你什么都没有?
  • 只需使用“Slow()”功能进行测试。将函数调用直接放在您的第一个 javascript sn-p 中。在 javascript 中,函数可以在定义之前被调用。测试后报告你得到的错误,如果有的话。

标签: javascript fckeditor


【解决方案1】:

你应该直接调用函数,如下例:

 <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”事件时,您必须考虑第二个代码 sn-p 的部分:

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);

这里的键码是注释中所说的箭头,您也可以从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);

【讨论】:

  • 只需在你需要的地方调用你需要的函数,就像我对'Slow()'所做的那样,它应该可以工作。
  • @xyz 我更改了答案,提供了一个有关如何更改代码的示例。希望这会有所帮助。
  • 如果你没有看到日志,说明代码没有被执行,所以检查句柄是否被调用和keycode值。在第一个'if'之前放置一个日志来调试它。
  • 您可以记录您想要的密钥的密钥代码,然后更改代码以执行某些操作。例如,this site 为您提供了一个快捷方式来捕获您需要的所有键的键码。
【解决方案2】:
var editor = document.getElementById("CKEditor1");
editor.on('contentDom', function () {
   editor.document.on('keydown', function (event) {
       if (event.data.key == 32)
           alert('space bar pressed');
 });
});

【讨论】:

  • 你应该改变你的问题,如果你没有答案就不要写答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2011-06-01
  • 2022-08-02
  • 1970-01-01
相关资源
最近更新 更多