我想用一個(gè)按鈕創(chuàng)建一個(gè)簡(jiǎn)單的用戶交互來(lái)開(kāi)始和停止錄制音頻,比如 whatsapp。我查看了 stackoverflow 以了解我是否錯(cuò)了,因?yàn)槲抑啦豢赡茉谕粋€(gè)元素上綁定兩個(gè)點(diǎn)擊事件,所以我決定在 codepen 上測(cè)試代碼,但它不會(huì)產(chǎn)生預(yù)期的結(jié)果:$('#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'); }});發(fā)生的事情是兩個(gè)事件同時(shí)登錄到控制臺(tái),但這不是我想要的,我只想在用戶錄制音頻時(shí)使用相同的按鈕開(kāi)始和停止錄音并更改圖標(biāo)。有沒(méi)有辦法做到這一點(diǎn)?
在單擊 jquery 上綁定兩個(gè)事件
慕田峪9158850
2021-10-14 13:05:39