jQuery 绑定解除绑定点击事件

发布于 2024-09-26 07:16:50 字数 779 浏览 0 评论 0原文

我正在尝试用 jQuery 来计算 ID 被点击的次数。 例如,如果单击 #kwick_1 一次,我希望它加载视频“否则”不执行任何操作。

我正在考虑在整个函数中应用这一点,因此 2、3、4 等也是如此。

我将如何实现这一目标?

var timesClicked = 0;
  $('#kwick_1, #kwick_2, #kwick_3, #kwick_4, #kwick_5, #kwick_6, #kwick_7').bind('click', function(event){
   var video = $(this).find('a').attr('href'); /* bunny.mp4 */

   var clickedID = $(this).attr('id'); /* kwick_1 */
   var vid = 'vid';
   timesClicked++;

   $(this).removeAttr('id').removeAttr('class').attr('id',clickedID + vid).attr('class',clickedID + vid);
   if(timesClicked == 1) {

    $.get("video.php", { video: video, poster: 'bunny.jpg', }, function(data){
     $('.'+clickedID+vid).html(data);
    }); 

   } else {
    /* Do nothing for now */
   }
   return false;
  });

I am trying to jQuery to calculate the amount of times a ID has been clicked.
for instance if #kwick_1 is clicked once, I want it to load the video 'else' do nothing.

And I am looking at applying this throughout the function so the same goes for 2, 3, 4 etc.

How would I achieve this?

var timesClicked = 0;
  $('#kwick_1, #kwick_2, #kwick_3, #kwick_4, #kwick_5, #kwick_6, #kwick_7').bind('click', function(event){
   var video = $(this).find('a').attr('href'); /* bunny.mp4 */

   var clickedID = $(this).attr('id'); /* kwick_1 */
   var vid = 'vid';
   timesClicked++;

   $(this).removeAttr('id').removeAttr('class').attr('id',clickedID + vid).attr('class',clickedID + vid);
   if(timesClicked == 1) {

    $.get("video.php", { video: video, poster: 'bunny.jpg', }, function(data){
     $('.'+clickedID+vid).html(data);
    }); 

   } else {
    /* Do nothing for now */
   }
   return false;
  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2024-10-03 07:16:50

您可以使用 .one() 来更轻松地做到这一点,例如这:

$('#kwick_1, #kwick_2, #kwick_3, #kwick_4, #kwick_5, #kwick_6, #kwick_7').one('click', function(event){
  var video = $(this).find('a').attr('href'), clicked = this.id + 'vid';

  $(this).attr({ id: clicked, 'class': clicked });
  $.get("video.php", { video: video, poster: 'bunny.jpg', }, function(data){
    $('.'+clicked).html(data);
  }); 
}).click(function() { return false; });

剩下的只是优化和减少重复代码,但想法是使用 .one(),那么该处理程序将在第一次执行后删除自身。然后我们使用 .click()return false >因为这种情况应该总是发生。确保像上面的示例一样之后绑定,因为事件处理程序按照它们绑定的顺序运行。

另外,如果您可以给它们全部一个类,您也可以用 .kwick 替换您的 #kwick_1, #kwick_2.... 选择器,上面的方法仍然有效并且更容易维持。

You can use .one() to do this a bit easier, like this:

$('#kwick_1, #kwick_2, #kwick_3, #kwick_4, #kwick_5, #kwick_6, #kwick_7').one('click', function(event){
  var video = $(this).find('a').attr('href'), clicked = this.id + 'vid';

  $(this).attr({ id: clicked, 'class': clicked });
  $.get("video.php", { video: video, poster: 'bunny.jpg', }, function(data){
    $('.'+clicked).html(data);
  }); 
}).click(function() { return false; });

The rest is just optimization and cutting down on repeated code, but the idea is to bind the part you only want to happen once using .one(), then that handler will remove itself after the first execution. Then we're binding the return false using .click() since that one should always happen. Make sure this is bound after like the example above, since event handlers are run in the order they were bound.

Also you could replace your #kwick_1, #kwick_2.... selector with .kwick if you could give them all a class, this above would still work and be much easier to maintain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文