javascript - 运行特定事件序列的按钮(我不知道如何简化的冗长代码)
问题描述
我的代码正在运行,但它非常冗长,而且我知道它可以变得更紧凑。我只是不知道怎么做:-)
我正在建立一个音频组合。单击按钮时,会发生一系列事件。单击另一个按钮时,所有其他活动按钮都将被终止,并且该特定按钮的序列将运行。
这些按钮是不可见的,并且放置在开关的可视化上。单击时,切换到“激活”状态的开关图像将删除其“显示:无”类别。这应该给用户一种实际轻弹开关开始播放音频的印象。
像这样:
$(function(){
// FIRST BUTTON
$('.button01').click(function() {
if ($('.switch01').hasClass('activated')){
// So if button.button01 is clicked and img.switch01 has class "activated"
// Kill all audio
$('audio').each(function(){ this.pause(); this.currentTime = 0; });
// Turn this switch off
$('.switch01').removeClass('activated');
// Kill all info cards showing the playback controls
$('.audio-info-card').addClass('d-none');
} else {
// If button.button01 is clicked and img.switch01 DOESN'T have class "activated"
// Turn all other switches off
$('.switch02, .switch03').removeClass('activated');
// Kill all info cards
$('.audio-info-card').addClass('d-none');
// Activate this switch and info card
$('.switch01').addClass('activated');
$('.audio-info-card#card01').removeClass('d-none');
// Kill all audio
$('audio').each(function(){ this.pause(); this.currentTime = 0; });
// Start this audio
$('#audio01-player')[0].play();
}
});
// SECOND BUTTON
$('.button02').click(function() {
if ($('.switch02').hasClass('activated')){
// So if button.button02 is clicked and img.switch02 has class "activated"
// Kill all audio
$('audio').each(function(){ this.pause(); this.currentTime = 0; });
// Turn this switch off
$('.switch02').removeClass('activated');
// Kill all info card showing the playback controls
$('.audio-info-card').addClass('d-none');
} else {
// If button.button02 is clicked and img.switch02 DOESN'T have class "activated"
// Turn all other switches off
$('.switch01, .switch03').removeClass('activated');
// Kill all info cards
$('.audio-info-card').addClass('d-none');
// Activate this switch and info card
$('.switch02').addClass('activated');
$('.audio-info-card#card02').removeClass('d-none');
// Kill all audio
$('audio').each(function(){ this.pause(); this.currentTime = 0; });
// Start this audio
$('#audio02-player')[0].play();
}
});
有 16 个按钮。我意识到这段代码很愚蠢,但 JS / jQuery 不是我的强项:-D
幸运的是,代码可以工作,但任何有助于简化此过程的帮助将不胜感激!
解决方案
click()
您可以为类名开头的所有按钮分配事件,button
如下所示:
$('button[class^="button"]').click(function() {
let buttonNr = $(this).attr("class").substr(6);
if ($('.switch' + buttonNr).hasClass('activated')) {
$('audio').each(function() {
this.pause();
this.currentTime = 0;
});
$('.switch' + buttonNr).removeClass('activated');
$('.audio-info-card').addClass('d-none');
} else {
$('[class^="switch"]').removeClass('activated');
$('.audio-info-card').addClass('d-none');
$('.switch' + buttonNr).addClass('activated');
$('.audio-info-card#card' + buttonNr).removeClass('d-none');
$('audio').each(function() {
this.pause();
this.currentTime = 0;
});
$('#audio' + buttonNr + '-player')[0].play();
}
});
推荐阅读
- python - 空图。怎么修?
- javascript - 创建和附加/删除与 display:block/display:none
- excel - 当excel列中的值发生变化时添加3个空白行
- javascript - React App 锚点外部链接不适用于移动设备的额外点击
- arrays - 如何计算数组中的行数?
- laravel - 如何避免 Package sebastian/resource-operations 被放弃,创建新的 laravel 应用程序时出错
- c++ - 为我指出正确的方向,以便在 C++ 中输入和输出鼠标点击
- python - 生成器/迭代器的输出在 for 循环之后消失了
- julia - 如何根据function和argtypes获取julia代码的exprs?
- flutter - 如何使堆栈中的孩子在颤动中的高度相等?