简体   繁体   English

在单击 jquery 上绑定两个事件

[英]bind two events on click jquery

I want to create a simple user interaction with a single button to start and stop recording audio,like whatsapp.我想用一个按钮创建一个简单的用户交互来开始和停止录制音频,比如 whatsapp。 I've looked on stackoverflow to understand if I was wrong, as I know it's not possible to bind on the same element two click events, so I've decided to test the code on codepen but it will not produce the expected result:我查看了 stackoverflow 以了解我是否错了,因为我知道不可能在同一个元素上绑定两个点击事件,所以我决定在 codepen 上测试代码,但它不会产生预期的结果:

$('#audioBtn').on('click' ,function(e){
  e.preventDefault();
  if( $(this).hasClass('active') ){
    $(this).removeClass('active')
    .addClass('inactive');
    console.log('Recording stopped');
  }
});

$('#audioBtn').on('click' , function(e){
  e.preventDefault();
  if( $(this).hasClass('inactive') ){
    $(this).addClass('active')
    .removeClass('inactive');
    console.log('Recording start');
  }
});

What happening is that the two events are logged on console at the same time, but this is not what I want, I just want to use the same button to start and stop the recordings and change the icon while the user is recording the audio.发生的事情是两个事件同时登录到控制台,但这不是我想要的,我只想在用户录制音频时使用相同的按钮开始和停止录音并更改图标。 Is there any way to do this?有没有办法做到这一点?

I know it's not possible to bind on the same element two click events我知道不可能在同一个元素上绑定两个点击事件

This is not the case, it's entirely possible to bind multiple event handlers for the same event type to a single element.事实并非如此,完全有可能将同一事件类型的多个事件处理程序绑定到单个元素。 The problem in your case is because the two handlers are conflicting with each other;您的情况的问题是因为两个处理程序相互冲突; one sets the class and the other detects the class and removes it.一个设置类,另一个检测类并删除它。

To fix this you need to use a single event handler which detects the state of the element and updates it based on that.要解决此问题,您需要使用单个事件处理程序来检测元素的状态并根据该状态更新它。 In your case a simple else statement will work.在您的情况下,一个简单的else语句将起作用。

$('#audioBtn').on('click', function(e) {
  e.preventDefault();
  if ($(this).hasClass('active')) {
    $(this).removeClass('active').addClass('inactive');
    console.log('Recording stopped');
  } else {
    $(this).addClass('active').removeClass('inactive');
    console.log('Recording start');
  }
});

Taking that a step further, you can use toggleClass() to swap the classes:更进一步,您可以使用toggleClass()来交换类:

 $('#audioBtn').on('click', function(e) { e.preventDefault(); if ($(this).hasClass('active')) { console.log('Recording stopped'); } else { console.log('Recording start'); } $(this).toggleClass('active inactive'); });
 .active { color: #C00; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="audioBtn">Start/Stop</button>

To keep it simple you could just toggleClass() the classes:为了简单toggleClass()您可以只toggleClass()类:

$('#audioBtn').on('click' , function(e){
  e.preventDefault();
  $(this).toggleClass('active');
});

I think only active class is enough.我认为只有活跃的课堂就足够了。 you can check wather active or not and simply toggle it.您可以检查是否处于活动状态并简单地切换它。 you can do any action by checking the id having active class or not.您可以通过检查是否具有活动类的 id 来执行任何操作。

 $('#audioBtn').on('click', function(e) { e.preventDefault(); if ($(this).hasClass('active')) { $(this).toggleClass('active'); console.log('Recording stopped'); } else { $(this).toggleClass('active'); console.log('Recording start'); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="audioBtn"> click </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM