Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Jquery或jJavascript中将三个事件绑定到同一个函数?_Javascript_Jquery_Keyboard Events_Jquery Events_Event Binding - Fatal编程技术网

如何在Jquery或jJavascript中将三个事件绑定到同一个函数?

如何在Jquery或jJavascript中将三个事件绑定到同一个函数?,javascript,jquery,keyboard-events,jquery-events,event-binding,Javascript,Jquery,Keyboard Events,Jquery Events,Event Binding,有人能解释三个事件如何绑定到同一个函数吗?i、 e.当发生以下事件时,应调用相同的函数 窗口卸载 按下“ESC”按钮时 单击“关闭”类 我用以下方式编写了单击“.close”类的函数: <script> $(document).ready(function() { var start = new Date().getTime(); var starttime = new Date(start); $(".close").click(function ()

有人能解释三个事件如何绑定到同一个函数吗?i、 e.当发生以下事件时,应调用相同的函数

  • 窗口卸载
  • 按下“ESC”按钮时
  • 单击“关闭”类
我用以下方式编写了单击“.close”类的函数:

<script>
$(document).ready(function() {
    var start = new Date().getTime();
    var starttime = new Date(start);
    $(".close").click(function () {
        jwplayer('mediaplayer').stop();
        end = new Date().getTime();
        endtime = new Date(end);            
        $.ajax({ 
          url: "/courses/136",
          data: {'timeSpent': endtime - starttime},
        });
    });

  });
</script>

$(文档).ready(函数(){
var start=new Date().getTime();
var starttime=新日期(开始);
$(“.close”)。单击(函数(){
jwplayer('mediaplayer').stop();
end=新日期().getTime();
结束时间=新日期(结束);
$.ajax({
网址:“/courses/136”,
数据:{'timespunt':endtime-starttime},
});
});
});

对于
window.unload()
和按ESC按钮时,也会发生同样的情况。是否有任何Jquery方法用于此

创建一个负责处理事件的函数,然后只需将该函数传递给要执行它的每个事件即可

<script>
  $(document).ready(function() {
    var start = new Date().getTime();
    var starttime = new Date(start);

    var eventHandler = function (event) {
        jwplayer('mediaplayer').stop();
        end = new Date().getTime();
        endtime = new Date(end);            
        $.ajax({ 
          url: "/courses/136",
          data: {'timeSpent': endtime - starttime},
        });
    };

    $(".close").click(eventHandler);
    $(window).on("unload", eventHandler);
    $(document).on("keydown", function(e) {
        if (e.which == 27) {
            eventHandler(e);
        }
    });
  });
</script>

$(文档).ready(函数(){
var start=new Date().getTime();
var starttime=新日期(开始);
var eventHandler=函数(事件){
jwplayer('mediaplayer').stop();
end=新日期().getTime();
结束时间=新日期(结束);
$.ajax({
网址:“/courses/136”,
数据:{'timespunt':endtime-starttime},
});
};
$(“.close”)。单击(eventHandler);
$(窗口).on(“卸载”,eventHandler);
$(文档).on(“向下键”,函数(e){
如果(e.which==27){
事件处理程序(e);
}
});
});

创建一个负责处理事件的函数,然后只需将该函数传递给要执行它的每个事件即可

<script>
  $(document).ready(function() {
    var start = new Date().getTime();
    var starttime = new Date(start);

    var eventHandler = function (event) {
        jwplayer('mediaplayer').stop();
        end = new Date().getTime();
        endtime = new Date(end);            
        $.ajax({ 
          url: "/courses/136",
          data: {'timeSpent': endtime - starttime},
        });
    };

    $(".close").click(eventHandler);
    $(window).on("unload", eventHandler);
    $(document).on("keydown", function(e) {
        if (e.which == 27) {
            eventHandler(e);
        }
    });
  });
</script>

$(文档).ready(函数(){
var start=new Date().getTime();
var starttime=新日期(开始);
var eventHandler=函数(事件){
jwplayer('mediaplayer').stop();
end=新日期().getTime();
结束时间=新日期(结束);
$.ajax({
网址:“/courses/136”,
数据:{'timespunt':endtime-starttime},
});
};
$(“.close”)。单击(eventHandler);
$(窗口).on(“卸载”,eventHandler);
$(文档).on(“向下键”,函数(e){
如果(e.which==27){
事件处理程序(e);
}
});
});

您只需定义函数:

function handler() {
    jwplayer('mediaplayer').stop();
    end = new Date().getTime();
    endtime = new Date(end);            
    $.ajax({ 
      url: "/courses/136",
      data: {'timeSpent': endtime - starttime},
    });
}
…并捆扎三次;对于ESC关键部件,您可能需要一个包装器:

$(".close").click(handler);
$(window).on("unload", handler);
$(document).on("keydown", function(e) { // Or whatever element is relevant
    if (e.which == 27) {
        handler.call(this, e);          // With the above, just `handler();` would work too
    }
});

您只需定义函数:

function handler() {
    jwplayer('mediaplayer').stop();
    end = new Date().getTime();
    endtime = new Date(end);            
    $.ajax({ 
      url: "/courses/136",
      data: {'timeSpent': endtime - starttime},
    });
}
…并捆扎三次;对于ESC关键部件,您可能需要一个包装器:

$(".close").click(handler);
$(window).on("unload", handler);
$(document).on("keydown", function(e) { // Or whatever element is relevant
    if (e.which == 27) {
        handler.call(this, e);          // With the above, just `handler();` would work too
    }
});

传递给jQuery方法(如
.click()
)的函数不必是匿名的。您可以按名称引用函数。因此:

function yourFunction() {
   // do your jwplayer and ajax thing here
}

$(window).on("unload", yourFunction);
$(".close").click(yourFunction);
$(document).on("keyup", function(e) {
  if (e.which == 27)
    yourFunction();
});

传递给jQuery方法(如
.click()
)的函数不必是匿名的。您可以按名称引用函数。因此:

function yourFunction() {
   // do your jwplayer and ajax thing here
}

$(window).on("unload", yourFunction);
$(".close").click(yourFunction);
$(document).on("keyup", function(e) {
  if (e.which == 27)
    yourFunction();
});
请发布完整答案,而不是发布半个答案,然后在完整版本中进行编辑。请发布完整答案,而不是发布半个答案,然后在完整版本中进行编辑。@BhojendraNepal:您不必这样做(请参阅注释)。如果处理程序使用了
this
(这个没有),您只需要使用
call
;如果处理程序使用了
e
(这个没有),您只需要通过
e
。但这显示了一种一般情况,即在调用时确保链接到的处理程序具有一致的环境。@BhojendraNepal:您不必这样做(请参阅注释)。如果处理程序使用了
this
(这个没有),您只需要使用
call
;如果处理程序使用了
e
(这个没有),您只需要通过
e
。但这显示了一个一般情况,即在调用时确保链接到的处理程序具有一致的环境。