5

我是 Javascript/jQuery 的新手,我正在尝试为 HTML5 视频播放制作一个播放按钮。到目前为止我的代码:

//return a jQuery object
var video = $('#myVideoTag');

//Play/Pause control clicked

$('.btnPlay').on('click', function() {
   if(video[0].paused) {
      video[0].play();
   }
   else {
      video[0].pause();
   }
   return false;
});

我不断收到此错误:

$(".btnPlay").on is not a function
[Break On This Error]   

$('.btnPlay').on('click', function() {

我终其一生都无法弄清楚出了什么问题。我在页面上正确定义了 .btnPlay 类。我正在关注一个似乎使用相同方法没有问题的教程。有任何想法吗?

4

3 回答 3

8

方法on是在 jQuery 版本 1.7 中引入的。

我认为您必须将您的 jQuery 库升级到最新版本

否则,您可以使用bind

$('.btnPlay').bind("click", function() {
    // ...
});

或其快捷方式click

$('.btnPlay').click(function() {
    // ...
});
于 2012-07-02T23:25:15.997 回答
1

As of jQuery 1.7, .delegate() has been superseded by the .on() method. For earlier versions, however, it remains the most effective means to use event delegation .delegate().

An other alternative is the .live() but since version 1.7 is deprecated. Users of older versions of jQuery should use .delegate() in preference to .live().

于 2014-02-28T13:45:49.637 回答
0

An alternative to the .on method on older versions of javascript is .delegate. You should use .delegate('.btnPlay', 'click', function(){...}) on the parent element to have the same functionality

于 2012-07-02T23:27:57.790 回答